Java String Methods Cheat Sheet
Based on common DSA patterns, here’s a complete reference of String methods frequently used in coding interviews and competitive programming.
1. Character Access & Information
Section titled “1. Character Access & Information”Method
Return Type
Description
Example
charAt(int index)
char
Get character at index
"Java".charAt(0) → ‘J’
length()
int
Get string length
"Hello".length() → 5
toCharArray()
char[]
Convert to char array
"abc".toCharArray() → [‘a’,‘b’,‘c’]
codePointAt(int index)
int
Get Unicode value
"A".codePointAt(0) → 65
isEmpty()
boolean
Check if empty
"".isEmpty() → true
isBlank()
boolean
Check if empty/whitespace (Java 11+)
" ".isBlank() → true
DSA Usage:
// Character frequency countingfor (char c : str.toCharArray()) { freq[c - 'a']++;}// Palindrome checkfor (int i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { return false; }}2. Search & Index Methods
Section titled “2. Search & Index Methods”Method
Return Type
Description
Example
indexOf(String str)
int
First occurrence index
"hello".indexOf("ll") → 2
indexOf(char ch)
int
First char occurrence
"hello".indexOf('e') → 1
indexOf(String str, int from)
int
Index from position
"hello".indexOf("l", 3) → 3
lastIndexOf(String str)
int
Last occurrence index
"hello".lastIndexOf("l") → 3
contains(CharSequence s)
boolean
Check if contains substring
"hello".contains("ell") → true
startsWith(String prefix)
boolean
Check prefix
"hello".startsWith("he") → true
endsWith(String suffix)
boolean
Check suffix
"hello".endsWith("lo") → true
DSA Usage:
// Pattern matchingif (text.indexOf(pattern) != -1) { // Pattern found}// File extension validationif (fileName.endsWith(".java")) { // Process Java file}3. Comparison Methods
Section titled “3. Comparison Methods”Method
Return Type
Description
Example
equals(Object obj)
boolean
Content equality
"abc".equals("abc") → true
equalsIgnoreCase(String str)
boolean
Case-insensitive equals
"ABC".equalsIgnoreCase("abc") → true
compareTo(String str)
int
Lexicographic comparison
"abc".compareTo("abd") → -1
compareToIgnoreCase(String str)
int
Case-insensitive compare
"ABC".compareToIgnoreCase("abc") → 0
contentEquals(CharSequence cs)
boolean
Compare with CharSequence
"test".contentEquals("test") → true
DSA Usage:
// Sorting stringsArrays.sort(strings, (a, b) -> a.compareTo(b));// Case-insensitive comparisonif ("Admin".equalsIgnoreCase(userRole)) { // Grant access}4. Substring & Extraction Methods
Section titled “4. Substring & Extraction Methods”Method
Return Type
Description
Example
substring(int begin)
String
Substring from begin to end
"Hello".substring(2) → “llo”
substring(int begin, int end)
String
Substring
DSA Usage:
// Generate all substringsfor (int i = 0; i < str.length(); i++) { for (int j = i + 1; j <= str.length(); j++) { String sub = str.substring(i, j); // Process substring }}// Sliding windowString window = str.substring(left, right + 1);5. Modification Methods (Return New String)
Section titled “5. Modification Methods (Return New String)”Method
Return Type
Description
Example
toLowerCase()
String
Convert to lowercase
"HELLO".toLowerCase() → “hello”
toUpperCase()
String
Convert to uppercase
"hello".toUpperCase() → “HELLO”
trim()
String
Remove leading/trailing spaces
" hi ".trim() → “hi”
strip()
String
Remove Unicode whitespace (Java 11+)
" hi ".strip() → “hi”
stripLeading()
String
Remove leading whitespace
" hi".stripLeading() → “hi”
stripTrailing()
String
Remove trailing whitespace
"hi ".stripTrailing() → “hi”
replace(char old, char new)
String
Replace all chars
"hello".replace('l', 'L') → “heLLo”
replace(CharSequence target, CharSequence replacement)
String
Replace all occurrences
"hello".replace("ll", "LL") → “heLLo”
replaceAll(String regex, String replacement)
String
Replace using regex
"a1b2".replaceAll("\\d", "X") → “aXbX”
replaceFirst(String regex, String replacement)
String
Replace first match
"a1b2".replaceFirst("\\d", "X") → “aXb2”
concat(String str)
String
Concatenate strings
"Hello".concat(" World") → “Hello World”
DSA Usage:
// Remove non-alphanumericString cleaned = str.replaceAll("[^a-zA-Z0-9]", "");// Case-insensitive operationsString lower = str.toLowerCase();// Remove all spacesString noSpaces = str.replaceAll("\\s+", "");6. Split & Join Methods
Section titled “6. Split & Join Methods”Method
Return Type
Description
Example
split(String regex)
String[]
Split by delimiter
"a,b,c".split(",") → [“a”,“b”,“c”]
split(String regex, int limit)
String[]
Split with limit
"a,b,c".split(",", 2) → [“a”,“b,c”]
String.join(CharSequence delimiter, CharSequence... elements)
String
Join with delimiter
String.join(",", "a", "b") → “a,b”
DSA Usage:
// Reverse wordsString[] words = str.split("\\s+");Collections.reverse(Arrays.asList(words));String reversed = String.join(" ", words);// CSV parsingString[] fields = line.split(",");7. Pattern Matching Methods
Section titled “7. Pattern Matching Methods”Method
Return Type
Description
Example
matches(String regex)
boolean
Match entire string
"123".matches("\\d+") → true
regionMatches(int toffset, String other, int ooffset, int len)
boolean
Compare regions
Check if regions match
DSA Usage:
// Validationif (email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")) { // Valid email}// Pattern checkingif (str.matches("[a-zA-Z]+")) { // Only letters}8. Conversion Methods
Section titled “8. Conversion Methods”Method
Return Type
Description
Example
String.valueOf(int i)
String
Convert to String
String.valueOf(123) → “123”
String.valueOf(char[] data)
String
Array to String
String.valueOf(new char[]{'a','b'}) → “ab”
String.valueOf(Object obj)
String
Object to String
Handles null safely
Integer.parseInt(String s)
int
String to int
Integer.parseInt("123") → 123
toString()
String
Return string itself
"hello".toString() → “hello”
getBytes()
byte[]
Convert to bytes
Get byte array
DSA Usage:
// Number to stringString numStr = String.valueOf(number);// String to number (atoi)int num = Integer.parseInt(str);// Character array to stringString s = String.valueOf(charArray);9. Format & Builder Methods
Section titled “9. Format & Builder Methods”Method
Return Type
Description
Example
String.format(String format, Object... args)
String
Formatted string
String.format("Age: %d", 25) → “Age: 25”
formatted(Object... args)
String
Format (Java 15+)
"Name: %s".formatted("John")
repeat(int count)
String
Repeat string (Java 11+)
"ab".repeat(3) → “ababab”
DSA Usage:
// Formatted outputString result = String.format("Result: %d/%d = %.2f", a, b, (double)a/b);// String repetitionString pattern = "01".repeat(5); // "0101010101"10. Advanced Methods
Section titled “10. Advanced Methods”Method
Return Type
Description
Example
intern()
String
Canonical string
Get from String Pool
hashCode()
int
Hash code
For hashing
lines()
Stream
Stream of lines (Java 11+)
Process lines
chars()
IntStream
Character stream
str.chars().forEach(...)
codePoints()
IntStream
Code point stream
Unicode processing
DSA Usage:
// String interning for comparison optimizationString s1 = new String("test").intern();String s2 = "test";// s1 == s2 → true// Stream processinglong count = str.chars().filter(c -> c == 'a').count();Quick Reference: Most Used in DSA
Section titled “Quick Reference: Most Used in DSA”Top 15 for Interviews:
-
length()- Almost every problem -
charAt(i)- Array-like access -
substring(i, j)- Subproblems -
toCharArray()- Manipulation -
indexOf()/lastIndexOf()- Pattern search -
equals()/equalsIgnoreCase()- Comparison -
compareTo()- Sorting -
toLowerCase()/toUpperCase()- Normalization -
trim()/strip()- Cleanup -
split()- Tokenization -
replace()/replaceAll()- Modification -
String.valueOf()- Conversion -
contains()- Substring check -
startsWith()/endsWith()- Prefix/suffix -
String.join()- Concatenation
Time Complexity Reference:
-
charAt(), length(), isEmpty(): O(1)
-
indexOf(), contains(): O(n)
-
substring(): O(n) where n = substring length
-
replace(), replaceAll(): O(n)
-
split(): O(n)
-
toLowerCase(), toUpperCase(): O(n)
-
compareTo(), equals(): O(n)