🚀 TechFlow Now

← Back to Resources

Java String Methods

length()

Returns the number of characters in the string.

String str = "Hello World";
System.out.println(str.length()); // 11
                

charAt(int index)

Returns the character at the specified index.

System.out.println(str.charAt(0)); // H
System.out.println(str.charAt(6)); // W
                

equals(Object obj)

Compares two strings for equality (case-sensitive).

String str2 = "hello world";
System.out.println(str.equals(str2)); // false
                

equalsIgnoreCase(String other)

Compares two strings for equality ignoring case.

System.out.println(str.equalsIgnoreCase(str2)); // true
                

substring(int beginIndex)

Returns substring from beginIndex to end of string.

System.out.println(str.substring(6)); // World
                

substring(int beginIndex, int endIndex)

Returns substring from beginIndex to endIndex (exclusive).

System.out.println(str.substring(0, 5)); // Hello
                

toLowerCase()

Converts string to lowercase.

System.out.println(str.toLowerCase()); // hello world
                

toUpperCase()

Converts string to uppercase.

System.out.println(str.toUpperCase()); // HELLO WORLD
                

trim()

Removes leading and trailing whitespaces.

String str3 = "   Java  ";
System.out.println(str3.trim()); // Java
                

replace(CharSequence target, CharSequence replacement)

Replaces all occurrences of target with replacement.

System.out.println(str.replace("World", "Java")); // Hello Java
                

replaceAll(String regex, String replacement)

Replaces each substring matching regex with replacement.

System.out.println(str.replaceAll("l", "L")); // HeLLo WorLd
                

replaceFirst(String regex, String replacement)

Replaces the first substring matching regex with replacement.

System.out.println(str.replaceFirst("l", "L")); // HeLlo World
                

contains(CharSequence s)

Checks if string contains the specified sequence.

System.out.println(str.contains("Hello")); // true
                

startsWith(String prefix)

Checks if string starts with the specified prefix.

System.out.println(str.startsWith("He")); // true
                

endsWith(String suffix)

Checks if string ends with the specified suffix.

System.out.println(str.endsWith("ld")); // true
                

indexOf(String str)

Returns index of first occurrence of substring.

System.out.println(str.indexOf("o")); // 4
                

lastIndexOf(String str)

Returns index of last occurrence of substring.

System.out.println(str.lastIndexOf("l")); // 9
                

isEmpty()

Checks if string length is 0.

String str4 = "";
System.out.println(str4.isEmpty()); // true
                

split(String regex)

Splits string into array based on regex.

String[] words = str.split(" ");
for(String word : words) { System.out.println(word); }
// Output: Hello
//         World
                

toCharArray()

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
                

concat(String str)

Concatenates string at the end of current string.

System.out.println(str.concat("!!!")); // Hello World!!!
                

valueOf(any type)

Converts any type to string.

int num = 100;
System.out.println(String.valueOf(num)); // "100"
                

intern()

Returns canonical representation of string.

String s1 = new String("Java").intern();
System.out.println(s1); // Java