Java String Methods Cheat Sheet
Method Use Example Output
length() Returns length of string "Hello".length() 5
charAt(i) Returns character at index "Hello".charAt(1) 'e'
toCharArray() Converts string to char array [Link]("Hi".toCharArray()) [H, i]
equals() Checks if two strings are equal "Java".equals("Java") true
equalsIgnoreCase() Compares ignoring case "Java".equalsIgnoreCase("java") true
compareTo() Lexicographic comparison "Java".compareTo("Apple") >0
contains() Checks if substring exists "program".contains("gram") true
indexOf() First occurrence index "Hello".indexOf("l") 2
lastIndexOf() Last occurrence index "Hello".lastIndexOf("l") 3
startsWith() Checks prefix "Hello".startsWith("He") true
endsWith() Checks suffix "Hello".endsWith("lo") true
substring() Extract substring "Welcome".substring(3,7) come
concat() Concatenates strings "Hi".concat(" There") Hi There
replace() Replaces characters "Java".replace('a', 'o') Jovo
replaceAll() Replaces using regex "Java".replaceAll("a","@") J@v@
trim() Removes leading/trailing spaces " Hi ".trim() Hi
toUpperCase() Converts to uppercase "hello".toUpperCase() HELLO
toLowerCase() Converts to lowercase "HELLO".toLowerCase() hello
split() Splits string by delimiter [Link]("a,b,c".split(",")) [a, b, c]
join() Joins strings with delimiter [Link]("-", "a","b","c") a-b-c
valueOf() Converts primitive to String [Link](10) 10
format() Formats string [Link]("Age: %d",20) Age: 20
isEmpty() Checks if empty "".isEmpty() true
isBlank() Checks blank/whitespace " ".isBlank() true
matches() Regex match "abc123".matches("[a-z]+\d+") true
intern() Returns from String pool new String("Hi").intern() "Hi"
repeat() Repeats string "Hi ".repeat(3) Hi Hi Hi