Java String CheatSheet
Java String CheatSheet
charAt(int index)
Returns the character at the specified index.
Example: str.charAt(2);
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();
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();