Java String Mastery Cheat Sheet
1. Java String Methods
length()
Returns the length of the string.
Example: str.length();
charAt(int index)
Returns the character at the specified index.
Example: str.charAt(2);
substring(int start, int end)
Returns a substring.
Example: str.substring(1, 4);
equals(String s)
Checks string equality.
Example: str.equals("Hello");
equalsIgnoreCase(String s)
Checks equality ignoring case.
Example: str.equalsIgnoreCase("hello");
compareTo(String s)
Compares two strings lexicographically.
Example: str.compareTo("apple");
indexOf(String s)
Returns index of first occurrence.
Example: str.indexOf("lo");
lastIndexOf(String s)
Returns last index of occurrence.
Example: str.lastIndexOf("l");
contains(CharSequence s)
Checks if string contains sequence.
Example: str.contains("ell");
toUpperCase()
Converts to uppercase.
Example: str.toUpperCase();
toLowerCase()
Converts to lowercase.
Example: str.toLowerCase();
trim()
Removes leading/trailing spaces.
Example: str.trim();
replace(char old, char new)
Replaces characters.
Example: str.replace("a", "o");
split(String regex)
Splits string around matches.
Example: str.split(" ");
toCharArray()
Converts string to char array.
Example: char[] chars = str.toCharArray();
isEmpty()
Checks if string is empty.
Example: str.isEmpty();
isBlank()
Checks if string is blank (Java 11+).
Example: str.isBlank();