String Methods in Java
length()
➡️Finds how many characters are present in the string.
Return type: int
Example:
String s = "Hello";
System.out.println(s.length()); // 5
equals()
➡️Compares two strings character by character (case-sensitive).
Return type: boolean
Example:
String a = "Java";
String b = "java";
System.out.println(a.equals(b)); // false
equalsIgnoreCase()
➡️Compares two strings but ignores case differences.
Return type: boolean
Example:
String a = "Java";
String b = "java";
System.out.println(a.equalsIgnoreCase(b)); // true
contains()
➡️Checks if a string contains a given sequence of characters.
Return type: boolean
Example:
String s = "Hello World";
System.out.println(s.contains("World")); // true
trim()
➡️Removes extra spaces from the beginning and end of the string.
Return type: String
Example:
String s = " Java ";
System.out.println(s.trim()); // "Java"
isEmpty()
➡️Checks if the string has no characters (length = 0).
Return type: boolean
Example:
String s = "";
System.out.println(s.isEmpty()); // true
toUpperCase()
➡️Converts all letters in the string to uppercase.
Return type: String
Example:
String s = "java";
System.out.println(s.toUpperCase()); // "JAVA"
toLowerCase()
➡️Converts all letters in the string to lowercase.
Return type: String
Example:
String s = "JAVA";
System.out.println(s.toLowerCase()); // "java"
indexOf()
➡️Returns the index of the first occurrence of a character or substring.
Return type: int
Example:
String s = "programming";
System.out.println(s.indexOf('g')); // 3
lastIndexOf()
➡️Returns the index of the last occurrence of a character or substring.
Return type: int
Example:
String s = "programming";
System.out.println(s.lastIndexOf('g')); // 10
startsWith()
➡️Checks if the string begins with the given word/characters.
Return type: boolean
Example:
String s = "Java Programming";
System.out.println(s.startsWith("Java")); // true
endsWith()
➡️Checks if the string ends with the given word/characters.
Return type: boolean
Example:
String s = "Java Programming";
System.out.println(s.endsWith("ing")); // true
replace()
➡️Replaces all occurrences of a character/word with another.
Return type: String
Example:
String s = "Java is fun";
System.out.println(s.replace("fun", "powerful")); // "Java is
powerful"
substring()
➡️Extracts a part of the string using start (and optional end) index.
Return type: String
Example:
String s = "Programming";
System.out.println(s.substring(3)); // "gramming"
System.out.println(s.substring(0, 6)); // "Progra"
compareTo()
➡️Compares two strings lexicographically (dictionary order).
Return type: int
Example:
System.out.println("Apple".compareTo("Banana")); // -1
charAt()
➡️Returns the character at a specific index.
Return type: char
Example:
String s = "Hello";
System.out.println(s.charAt(1)); // 'e'
toCharArray()
➡️Converts the whole string into a character array.
Return type: char[]
Example:
String s = "Java";
char[] arr = s.toCharArray();
for(char c : arr) {
System.out.print(c + " "); // J a v a
}
split()
➡️Breaks the string into parts based on a given separator.
Return type: String[]
Example:
String s = "apple,banana,orange";
String[] fruits = s.split(",");
for(String f : fruits) {
System.out.println(f);
}