Java String Methods Cheat Sheet
length()
Example: str.length();
charAt(int index)
Example: str.charAt(0);
substring(int beginIndex)
Example: str.substring(2);
substring(int beginIndex, int endIndex)
Example: str.substring(2, 5);
equals(String another)
Example: str1.equals(str2);
equalsIgnoreCase(String another)
Example: str1.equalsIgnoreCase(str2);
isEmpty()
Example: str.isEmpty();
isBlank()
Example: str.isBlank(); // Java 11+
trim()
Example: str.trim();
toLowerCase()
Example: str.toLowerCase();
toUpperCase()
Example: str.toUpperCase();
concat(String str)
Example: str.concat("World");
indexOf(String str)
Example: str.indexOf("a");
lastIndexOf(String str)
Example: str.lastIndexOf("a");
contains(CharSequence s)
Example: str.contains("text");
startsWith(String prefix)
Example: str.startsWith("He");
endsWith(String suffix)
Example: str.endsWith("ld");
matches(String regex)
Example: str.matches("\\d+");
replace(char oldChar, char newChar)
Example: str.replace('a', 'b');
replaceAll(String regex, String replacement)
Example: str.replaceAll("a", "b");
replaceFirst(String regex, String replacement)
Example: str.replaceFirst("a", "b");
strip()
Example: str.strip(); // Java 11+
stripLeading()
Example: str.stripLeading();
stripTrailing()
Example: str.stripTrailing();
split(String regex)
Example: str.split(" ");
toCharArray()
Example: char[] arr = str.toCharArray();
valueOf(int i)
Example: String.valueOf(100);
compareTo(String another)
Example: str1.compareTo(str2);
intern()
Example: str.intern();
repeat(int count)
Example: str.repeat(3); // Java 11+