Returns the number of characters in the string.
String str = "Hello World";
System.out.println(str.length()); // 11
Returns the character at the specified index.
System.out.println(str.charAt(0)); // H
System.out.println(str.charAt(6)); // W
Compares two strings for equality (case-sensitive).
String str2 = "hello world";
System.out.println(str.equals(str2)); // false
Compares two strings for equality ignoring case.
System.out.println(str.equalsIgnoreCase(str2)); // true
Returns substring from beginIndex to end of string.
System.out.println(str.substring(6)); // World
Returns substring from beginIndex to endIndex (exclusive).
System.out.println(str.substring(0, 5)); // Hello
Converts string to lowercase.
System.out.println(str.toLowerCase()); // hello world
Converts string to uppercase.
System.out.println(str.toUpperCase()); // HELLO WORLD
Removes leading and trailing whitespaces.
String str3 = " Java ";
System.out.println(str3.trim()); // Java
Replaces all occurrences of target with replacement.
System.out.println(str.replace("World", "Java")); // Hello Java
Replaces each substring matching regex with replacement.
System.out.println(str.replaceAll("l", "L")); // HeLLo WorLd
Replaces the first substring matching regex with replacement.
System.out.println(str.replaceFirst("l", "L")); // HeLlo World
Checks if string contains the specified sequence.
System.out.println(str.contains("Hello")); // true
Checks if string starts with the specified prefix.
System.out.println(str.startsWith("He")); // true
Checks if string ends with the specified suffix.
System.out.println(str.endsWith("ld")); // true
Returns index of first occurrence of substring.
System.out.println(str.indexOf("o")); // 4
Returns index of last occurrence of substring.
System.out.println(str.lastIndexOf("l")); // 9
Checks if string length is 0.
String str4 = "";
System.out.println(str4.isEmpty()); // true
Splits string into array based on regex.
String[] words = str.split(" ");
for(String word : words) { System.out.println(word); }
// Output: Hello
// World
Converts string to char array.
char[] chars = str.toCharArray();
for(char c : chars) { System.out.print(c + " "); }
// H e l l o W o r l d
Concatenates string at the end of current string.
System.out.println(str.concat("!!!")); // Hello World!!!
Converts any type to string.
int num = 100;
System.out.println(String.valueOf(num)); // "100"
Returns canonical representation of string.
String s1 = new String("Java").intern();
System.out.println(s1); // Java