Java String Functions Cheat Sheet
Method Purpose Example
length() Returns string length [Link]()
trim() Removes leading/trailing spaces [Link]()
toUpperCase() Converts to uppercase [Link]()
toLowerCase() Converts to lowercase [Link]()
equals(str2) Checks equality (case-sensitive) [Link](str2)
equalsIgnoreCase(str2) Checks equality ignoring case [Link](str2)
charAt(index) Returns char at index [Link](2)
indexOf(substring) Returns first index of substring [Link]("Java")
lastIndexOf(substring) Returns last index of substring [Link]("o")
contains(substring) Checks if substring exists [Link]("World")
startsWith(prefix) Checks if starts with prefix [Link]("He")
endsWith(suffix) Checks if ends with suffix [Link]("ld")
substring(begin) Substring from begin index [Link](2)
substring(begin, end) Substring in range [Link](2, 7)
replace(old, new) Replaces old char/word [Link]("Java", "Python")
replaceAll(regex, new) Replaces using regex [Link](" ", "_")
split(regex) Splits string by regex [Link](" ")
isEmpty() Checks if string is empty [Link]()
matches(regex) Checks if matches regex [Link](".*Java.*")
compareTo(str2) Lexicographic compare "apple".compareTo("banana")
join(delimiter, ...) Joins multiple strings [Link]("-", "A", "B")
repeat(count) Repeats string "Hi".repeat(3)
strip() Removes all spaces (Unicode) [Link]()
stripLeading() Removes leading spaces [Link]()
stripTrailing() Removes trailing spaces [Link]()
format(format, args...) Formats string [Link]("%s %d", "Age", 25)
valueOf(var) Converts var to String [Link](123)