Welcome to your go-to Java String Manipulation cheatsheet. Below you’ll find the most commonly used String methods in Java, complete with examples to help you write clean, efficient code and boost your blog’s search visibility.
Java String Manipulation Cheatsheet
🗒️ Created by: Lamhot Siagian 🔗
Java String Manipulation Quick Reference
📋 Inspection & Info
Method
Description
length()
Number of characters
isEmpty()
True if length == 0
🔠 Character Access & Code Points
Method
Description
charAt(int index)
Character at index
codePointAt(int index)
Unicode code point at index
codePointCount(int begin, int end)
Count code points in range
offsetByCodePoints(int index, int offset)
Index shift by code points
⚖️ Comparison & Matching
Method
Description
equals(Object other)
Case-sensitive equality
equalsIgnoreCase(String other)
Case-insensitive equality
compareTo(String another)
Lexicographical compare
matches(String regex)
Match entire string to regex
regionMatches(…)
Compare substrings by region
🔍 Searching
Method
Description
contains(CharSequence s)
Check substring existence
indexOf(String str)/lastIndexOf(String str)
First/last occurrence
startsWith(String prefix)/endsWith(String suffix)
Check prefix/suffix
✂️ Extraction & Substrings
Method
Description
substring(int begin)
From begin to end
substring(int begin, int end)
Between begin (inclusive) and end (exclusive)
subSequence(int start, int end)
Return as CharSequence
🔤 Case Conversion & Trimming
Method
Description
toLowerCase()/toUpperCase()
Convert case
trim()/strip()
Remove leading/trailing whitespace
🛠️ Replacement & Modification
Method
Description
concat(String str)
Append string
replace(oldChar, newChar)
Replace chars
replaceAll(regex, replacement)
Replace via regex
repeat(int count)
Repeat string (Java 11+)
🪡 Splitting & Lines
Method
Description
split(String regex)
Split into array
split(String regex, int limit)
Split with limit
lines()
Stream of lines (Java 11+)
🔗 Conversion & Encoding
Method
Description
toCharArray()
Convert to char array
getBytes()/getBytes(Charset)
Get bytes
intern()
Return string from pool
📊 Streaming (Java 8+)
Method
Description
chars()
Stream of UTF‑16 code units
codePoints()
Stream of Unicode code points
🎨 Formatting & Joining
Method
Description
format(String fmt, Object…)
Static printf‑style formatting
formatted(Object…)
Instance formatting (Java 15+)
join(CharSequence, CharSequence…)
Join with delimiter
🧰 Static Utilities
Method
Description
valueOf(Object obj)
Convert any value to String
copyValueOf(char[] data)
Convert char array to String
Inspection & Info
Method
Description
int length()
Returns number of characters
boolean isEmpty()
Returns true if length == 0
Character Access & Code Points
Method
Description
char charAt(int index)
Character at given index
int codePointAt(int index)
Unicode code point at index
int codePointCount(int beginIndex, int endIndex)
Number of Unicode code points in range
int offsetByCodePoints(int index, int codePointOffset)
Index shift by code points
Comparison & Matching
Method
Description
boolean equals(Object other)
Case-sensitive equality
boolean equalsIgnoreCase(String other)
Case-insensitive equality
int compareTo(String another)
Lexicographical compare
boolean matches(String regex)
Full-match regex
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Leave a Reply