charAt(int index)
charAt(int index): Returns the character at the specified index.
String str = "Hello";
char ch = str.charAt(1); // 'e'
compareTo(String anotherString)
compareTo(String anotherString): Compares two strings lexicographically.
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2); // negative value
compareToIgnoreCase(String anotherString)
compareToIgnoreCase(String anotherString): Compares two strings lexicographically, ignoring case differences.
String str1 = "hello";
String str2 = "HELLO";
int result = str1.compareToIgnoreCase(str2); // 0
concat(String str)
concat(String str): Concatenates the specified string to the end of this string.
String str1 = "Hello, ";
String str2 = "World!";
String result = str1.concat(str2); // "Hello, World!"
contains(CharSequence sequence)
contains(CharSequence sequence): Returns true if this string contains the specified sequence of char values.
String str = "Hello, World!";
boolean contains = str.contains("World"); // true
endsWith(String suffix)
endsWith(String suffix): Tests if this string ends with the specified suffix.
String str = "Hello.txt";
boolean endsWith = str.endsWith(".txt"); // true
equals(Object anObject)
equals(Object anObject): Compares this string to the specified object.
String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // true
equalsIgnoreCase(String anotherString)
equalsIgnoreCase(String anotherString): Compares this string to another string, ignoring case considerations.
String str1 = "hello";
String str2 = "HELLO";
boolean isEqual = str1.equalsIgnoreCase(str2); // true
indexOf(int ch)
indexOf(int ch): Returns the index of the first occurrence of the specified character.
String str = "Hello";
int index = str.indexOf('e'); // 1
indexOf(String str)
indexOf(String str): Returns the index of the first occurrence of the specified substring.
String str = "Hello, World!";
int index = str.indexOf("World"); // 7
isEmpty()
isEmpty(): Returns true if this string is empty.
String str = "";
boolean isEmpty = str.isEmpty(); // true
lastIndexOf(int ch)
lastIndexOf(int ch): Returns the index of the last occurrence of the specified character.
String str = "Hello, World!";
int lastIndex = str.lastIndexOf('o'); // 8
lastIndexOf(String str)
lastIndexOf(String str): Returns the index of the last occurrence of the specified substring.
String str = "Hello, World! Hello!";
int lastIndex = str.lastIndexOf("Hello"); // 13
length()
length(): Returns the length of this string.
String str = "Hello";
int length = str.length(); // 5
replace(char oldChar, char newChar)
replace(char oldChar, char newChar): Returns a new string resulting from replacing all occurrences of oldChar with newChar.
String str = "Hello";
String result = str.replace('l', 'p'); // "Heppo"
replace(String target, String replacement)
replace(String target, String replacement): Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String str = "Hello, World!";
String result = str.replace("World", "Java"); // "Hello, Java!"
split(String regex)
split(String regex): Splits this string around matches of the given regular expression.
String str = "apple,banana,cherry";
String[] fruits = str.split(","); // ["apple", "banana", "cherry"]
startsWith(String prefix)
startsWith(String prefix): Tests if this string starts with the specified prefix.
String str = "Hello, World!";
boolean startsWith = str.startsWith("Hello"); // true
substring(int beginIndex)
substring(int beginIndex): Returns a new string that is a substring of this string.
String str = "Hello, World!";
String sub = str.substring(7); // "World!"