Common Java String methods ( AI Generated)
Here's a list of common String methods and functions in Java programming:
1. length(): Returns the length of the string
2. charAt(int index): Returns the character at the specified index
3. substring(int beginIndex): Returns a substring from the specified index to the end
4. substring(int beginIndex, int endIndex): Returns a substring from beginIndex to endIndex-1
5. toLowerCase(): Converts the string to lowercase
6. toUpperCase(): Converts the string to uppercase
7. trim(): Removes leading and trailing whitespace
8. replace(char oldChar, char newChar): Replaces all occurrences of oldChar with newChar
9. replaceAll(String regex, String replacement): Replaces all substrings matching the regex
10. split(String regex): Splits the string around matches of the given regex
11. contains(CharSequence sequence): Checks if the string contains the specified sequence
12. startsWith(String prefix): Checks if the string starts with the specified prefix
13. endsWith(String suffix): Checks if the string ends with the specified suffix
14. equals(Object obj): Compares the string with another object for equality
15. equalsIgnoreCase(String anotherString): Compares strings, ignoring case
16. compareTo(String anotherString): Compares two strings lexicographically
17. indexOf(String str): Returns the index of the first occurrence of str
18. lastIndexOf(String str): Returns the index of the last occurrence of str
19. concat(String str): Concatenates the specified string to the end of this string
20. isEmpty(): Checks if the string is empty
21. valueOf(various types): Returns the string representation of the argument
These methods provide a wide range of functionality for manipulating and working with strings in Java.
public class StringExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println("Length: " + str.length()); // Output: 11
System.out.println("Character at index 0: " + str.charAt(0)); // Output: H
System.out.println("Substring from index 6: " + str.substring(6)); // Output: World
System.out.println("Substring from index 0 to 5: " + str.substring(0, 5)); // Output: Hello
System.out.println("Concatenation: " + str.concat("!")); // Output: Hello World!
System Engineering.out.println("Replace 'o' with 'a': " + str.replace('o', 'a')); // Output: Hella Warld
System.out.println("Uppercase: " + str.toUpperCase()); // Output: HELLO WORLD
System.out.println("Lowercase: " + str.toLowerCase()); // Output: hello world
System.out.println("Contains 'World': " + str.contains("World")); // Output: true
System.out.println("Equals 'Hello World': " + str.equals("Hello World")); // Output: true
System.out.println("Index of 'World': " + str.indexOf("World")); // Output: 6
}
}
Citations:
[1] https://www.w3schools.com/java/java_ref_string.asp
[2] https://javaconceptoftheday.com/java-new-string-methods-with-examples/
[3] https://www.javatpoint.com/methods-of-string-class
[4] http://cs111.wellesley.edu/~cs111/archive/cs111_fall06/public_html/labs/lab9/lab9.html
[5] https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
[6] https://docs.oracle.com/javase/10/docs/api/java/lang/String.html
[7] https://www.geeksforgeeks.org/string-class-in-java/
[8] https://www.programiz.com/java-programming/string
(Generated using the perplexity ai )
Comments
Post a Comment