Java String Methods Cheat Sheet
Java String Methods Cheat Sheet
rameshfadatare.com/cheat-sheet/java-string-methods-cheat-sheet
Introduction
Java’s String class provides a comprehensive set of methods for string manipulation,
comparison, searching, and more. Understanding these methods is crucial for effective
string handling in Java applications. This cheat sheet lists all the String methods
available in Java SE 22, providing a brief description, examples, and explanations.
Method Description
charAt(int index) Returns the character at the specified index.
endsWith(String suffix) Tests if this string ends with the specified suffix.
1/18
Method Description
getChars(int srcBegin, int Copies characters from this string into the
srcEnd, char[] dst, int destination character array.
dstBegin)
indexOf(int ch) Returns the index within this string of the first
occurrence of the specified character.
indexOf(int ch, int Returns the index within this string of the first
fromIndex) occurrence of the specified character, starting the
search at the specified index.
indexOf(String str) Returns the index within this string of the first
occurrence of the specified substring.
indexOf(String str, int Returns the index within this string of the first
fromIndex) occurrence of the specified substring, starting at
the specified index.
lastIndexOf(int ch) Returns the index within this string of the last
occurrence of the specified character.
lastIndexOf(int ch, int Returns the index within this string of the last
fromIndex) occurrence of the specified character, searching
backward starting at the specified index.
lastIndexOf(String str) Returns the index within this string of the last
occurrence of the specified substring.
lastIndexOf(String str, int Returns the index within this string of the last
fromIndex) occurrence of the specified substring, searching
backward starting at the specified index.
2/18
Method Description
matches(String regex) Tells whether or not this string matches the given
regular expression.
offsetByCodePoints(int index, Returns the index within this String that is offset
int codePointOffset) from the given index by codePointOffset code
points.
split(String regex, int Splits this string around matches of the given
limit) regular expression.
startsWith(String prefix) Tests if this string starts with the specified prefix.
3/18
Method Description
charAt(int index)
Explanation: This method returns the character located at index 1 in the string "Hello".
codePointAt(int index)
Explanation: This method returns the Unicode code point of the character at index 1 in
the string "Hello".
4/18
codePointBefore(int index)
Description: Returns the Unicode code point before the specified index.
Example:
Explanation: This method returns the Unicode code point of the character before index 1
in the string "Hello".
Description: Returns the number of Unicode code points in the specified text range.
Example:
Explanation: This method returns the number of Unicode code points between the
specified begin and end indexes.
compareTo(String anotherString)
Explanation: This method compares the strings "Hello" and "World" lexicographically.
compareToIgnoreCase(String str)
concat(String str)
5/18
Explanation: This method appends the string str2 to the end of str1.
contains(CharSequence s)
Description: Returns true if this string contains the specified sequence of char values.
Example:
Explanation: This method checks if the string "Hello World" contains the sequence
"World".
contentEquals(CharSequence cs)
Explanation: This method compares the string with the StringBuffer content and
returns true if they are equal.
copyValueOf(char[] data)
Description: Returns a String that represents the character sequence in the array.
Example:
Explanation: This method creates a string from the character array data.
endsWith(String suffix)
Explanation: This method checks if the string "Hello World" ends with the suffix "World".
equals(Object anObject)
6/18
Explanation: This method compares the string str1 with the object str2 for equality.
equalsIgnoreCase(String anotherString)
Explanation: This method compares the strings str1 and str2 for equality, ignoring case
differences.
Description: Returns a formatted string using the specified format string and arguments.
Example:
String str = String.format("Name: %s, Age: %d", "John", 30); // Name: John, Age:
30
Explanation: This method formats the string according to the specified format and
arguments.
getBytes()
Description: Encodes this String into a sequence of bytes using the platform’s default
charset.
Example:
Explanation: This method converts the string to a byte array using the default charset.
getBytes(String charsetName)
Description: Encodes this String into a sequence of bytes using the named charset.
Example:
Explanation: This method converts the string to a byte array using the specified charset
"UTF-8".
Description: Copies characters from this string into the destination character array.
Example:
7/18
String str = "Hello World";
char[] dst = new char[5];
str.getChars(0, 5, dst, 0); // dst contains "Hello"
Explanation: This method copies characters from the string to the character array dst.
hashCode()
Explanation: This method returns the hash code value for the string.
indexOf(int ch)
Description: Returns the index within this string of the first occurrence of the specified
character.
Example:
Explanation: This method returns the index of the first occurrence of the character ‘o’ in
the string.
Description: Returns the index within this string of the first occurrence of the specified
character, starting the search at the specified index.
Example:
Explanation: This method returns the index of the first occurrence of the character ‘o’ in
the string, starting from index 5.
indexOf(String str)
Description: Returns the index within this string of the first occurrence of the specified
substring.
Example:
Explanation: This method returns the index of the first occurrence of the substring
"World" in the string.
8/18
indexOf(String str, int fromIndex)
Description: Returns the index within this string of the first occurrence of the specified
substring, starting at the specified index.
Example:
Explanation: This method returns the index of the first occurrence of the substring
"World" in the string, starting from index 7.
intern()
Explanation: This method returns a canonical representation of the string, ensuring that
strings with the same content share the same memory.
isBlank()
Description: Returns true if the string is empty or contains only white space codepoints.
Example:
Explanation: This method checks if the string is empty or consists only of white space
characters.
isEmpty()
9/18
Explanation: This method joins the elements "A", "B", and "C" with the delimiter ", ".
lastIndexOf(int ch)
Description: Returns the index within this string of the last occurrence of the specified
character.
Example:
Explanation: This method returns the index of the last occurrence of the character ‘o’ in
the string.
Description: Returns the index within this string of the last occurrence of the specified
character, searching backward starting at the specified index.
Example:
Explanation: This method returns the index of the last occurrence of the character ‘o’ in
the string, searching backward from index 6.
lastIndexOf(String str)
Description: Returns the index within this string of the last occurrence of the specified
substring.
Example:
Explanation: This method returns the index of the last occurrence of the substring
"World" in the string.
Description: Returns the index within this string of the last occurrence of the specified
substring, searching backward starting at the specified index.
Example:
Explanation: This method returns the index of the last occurrence of the substring
"World" in the string, searching backward from index 11.
length()
10/18
Description: Returns the length of this string.
Example:
lines()
Description: Returns a stream of lines extracted from this string, separated by line
terminators.
Example:
Explanation: This method splits the string into lines based on line terminators and
returns a stream of those lines.
matches(String regex)
Description: Tells whether or not this string matches the given regular expression.
Example:
Explanation: This method checks if the string "123-45-6789" matches the regular
expression for a social security number format.
Description: Returns the index within this String that is offset from the given index by
codePointOffset code points.
Example:
Explanation: This method returns the index offset by 2 code points from the beginning of
the string.
11/18
String str1 = "Hello World";
String str2 = "WORLD";
boolean result = str1.regionMatches(true, 6, str2, 0, 5); // true
Explanation: This method checks if the region "World" in str1 matches "WORLD" in
str2 ignoring case.
repeat(int count)
Description: Returns a string whose value is the concatenation of this string repeated
count times.
Example:
Description: Returns a string resulting from replacing all occurrences of oldChar in this
string with newChar.
Example:
Explanation: This method replaces all occurrences of the character ‘l’ with ‘p’ in the
string.
Description: Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence.
Example:
Explanation: This method replaces the substring "World" with "Java" in the string.
Description: Replaces each substring of this string that matches the given regular
expression with the given replacement.
Example:
Explanation: This method replaces all digits in the string with hyphens.
12/18
replaceFirst(String regex, String replacement)
Description: Replaces the first substring of this string that matches the given regular
expression with the given replacement.
Example:
Explanation: This method replaces the first digit in the string with a hyphen.
split(String regex)
Description: Splits this string around matches of the given regular expression.
Example:
Explanation: This method splits the string "one,two,three" into an array of substrings.
Description: Splits this string around matches of the given regular expression, with a limit
on the number of resulting substrings.
Example:
Explanation: This method splits the string into at most two substrings.
startsWith(String prefix)
Explanation: This method checks if the string starts with the prefix "Hello".
Description: Tests if the substring of this string beginning at the specified index starts
with the specified prefix.
Example:
Explanation: This method checks if the substring starting at index 6 starts with "World".
13/18
strip()
Description: Returns a string whose value is this string, with all leading and trailing white
space removed.
Example:
Explanation: This method removes all leading and trailing white space from the string.
stripLeading()
Description: Returns a string whose value is this string, with all leading white space
removed.
Example:
Explanation: This method removes all leading white space from the string.
stripTrailing()
Description: Returns a string whose value is this string, with all trailing white space
removed.
Example:
Explanation: This method removes all trailing white space from the string.
substring(int beginIndex)
14/18
Explanation: This method returns the substring starting from index 6 to the end of the
string.
toCharArray()
toLowerCase()
Description: Converts all of the characters in this String to lower case using the rules of
the default locale.
Example:
Explanation: This method converts all characters in the string to lower case using the
default locale.
toLowerCase(Locale locale)
Description: Converts all of the characters in this String to lower case using the rules of
the given Locale.
Example:
Explanation: This method converts all characters in the string to lower case using the
specified locale.
toUpperCase()
Description: Converts all of the characters in this String to upper case using the rules of
the default locale.
Example:
15/18
String str = "hello";
String result = str.toUpperCase(); // HELLO
Explanation: This method converts all characters in the string to upper case using the
default locale.
toUpperCase(Locale locale)
Description: Converts all of the characters in this String to upper case using the rules of
the given Locale.
Example:
Explanation: This method converts all characters in the string to upper case using the
specified locale.
trim()
Description: Returns a string whose value is this string, with all leading and trailing
space removed.
Example:
Explanation: This method removes all leading and trailing space characters from the
string.
valueOf(char[] data)
Explanation: This method creates a new string that represents the sequence of
characters in the array data.
Description: Returns the string representation of a subarray of the char array argument.
Example:
Explanation: This method creates a new string that represents a subarray of the char
array data starting at offset and extending count characters.
16/18
valueOf(boolean b)
Explanation: This method creates a new string that represents the boolean value b.
valueOf(char c)
char c = 'A';
String result = String.valueOf(c); // A
Explanation: This method creates a new string that represents the character c.
valueOf(int i)
int i = 123;
String result = String.valueOf(i); // 123
Explanation: This method creates a new string that represents the integer i.
valueOf(long l)
long l = 123L;
String result = String.valueOf(l); // 123
Explanation: This method creates a new string that represents the long value l.
valueOf(float f)
float f = 3.14f;
String result = String.valueOf(f); // 3.14
Explanation: This method creates a new string that represents the float value f.
valueOf(double d)
17/18
Description: Returns the string representation of the double argument.
Example:
double d = 3.14159;
String result = String.valueOf(d); // 3.14159
Explanation: This method creates a new string that represents the double value d.
Conclusion
This comprehensive cheat sheet provides a quick reference to all the Java String
methods, their descriptions, examples, and explanations. Mastering these methods will
enable you to handle strings more effectively in your Java programs. Keep this guide
handy to improve your efficiency and understanding of string manipulation in Java. Happy
coding!
18/18