Skip to content

Here are the 20 most important Java String methods in an interview-friendly cheat sheet.

Method Syntax Return Type Primary Purpose Example Usage Result
str.length() int Returns the number of characters in the string "Java".length() 4
str.charAt(index) char Returns the character at the specified index "Java".charAt(2) 'v'
str.substring(beginIndex) String Returns substring from the given index to the end "Java".substring(2) "va"
str.substring(beginIndex, endIndex) String Returns substring between two indexes (endIndex excluded) "Java".substring(1,3) "av"
str.indexOf(value) int Returns the index of the first occurrence "Java".indexOf("a") 1
str.lastIndexOf(value) int Returns the index of the last occurrence "Java".lastIndexOf("a") 3
str.contains(sequence) boolean Checks whether the string contains a sequence of characters "Java".contains("av") true
str.equals(str2) boolean Compares two strings for exact equality (case-sensitive) "Java".equals("java") false
str.equalsIgnoreCase(str2) boolean Compares two strings ignoring case "Java".equalsIgnoreCase("java") true
str.compareTo(str2) int Lexicographically compares two strings "Apple".compareTo("Banana") Negative value
str.split(regex) String[] Splits the string into an array using a delimiter "A,B,C".split(",") {"A","B","C"}
str.replace(oldValue, newValue) String Replaces all occurrences of a character or string "Java".replace("a","o") "Jovo"
str.replaceAll(regex, replacement) String Replaces all substrings matching a regular expression "a1b2".replaceAll("\\d","") "ab"
str.startsWith(prefix) boolean Checks whether the string starts with the specified prefix "Java".startsWith("Ja") true
str.endsWith(suffix) boolean Checks whether the string ends with the specified suffix "Java".endsWith("va") true
str.trim() String Removes leading and trailing whitespace " Java ".trim() "Java"
str.toUpperCase() String Converts all characters to uppercase "java".toUpperCase() "JAVA"
str.toLowerCase() String Converts all characters to lowercase "JAVA".toLowerCase() "java"
str.toCharArray() char[] Converts the string into a character array "Java".toCharArray() {'J','a','v','a'}
str.isEmpty() boolean Checks whether the string length is 0 "".isEmpty() true
str.concat(str2) String Appends one string to another "Java".concat("8") "Java8"

⭐ Interview Quick Notes¢

Method Time Complexity Interview Tip
length() O(1) Most frequently used method.
charAt() O(1) Used in almost every string coding problem.
substring() O(n) endIndex is exclusive.
indexOf() O(n) Returns -1 if not found.
lastIndexOf() O(n) Searches from the end.
contains() O(n) Returns true if the substring exists.
equals() O(n) Always use for string comparison, not ==.
equalsIgnoreCase() O(n) Ignores uppercase/lowercase differences.
compareTo() O(n) Returns 0, positive, or negative.
split() O(n) Returns a String[]; uses regular expressions.
replace() O(n) Does not use regex.
replaceAll() O(n) Uses regex; slower than replace().
startsWith() O(m) m = prefix length.
endsWith() O(m) m = suffix length.
trim() O(n) Removes only leading and trailing spaces.
toUpperCase() O(n) Returns a new string.
toLowerCase() O(n) Returns a new string.
toCharArray() O(n) Common in DSA questions.
isEmpty() O(1) Equivalent to length() == 0.
concat() O(n + m) Creates a new string since String is immutable.

These 20 methods cover the vast majority of String operations asked in Java coding interviews and day-to-day development.