Java String Manipulation Cheatsheet

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
MethodDescription
length()Number of characters
isEmpty()True if length == 0
🔠 Character Access & Code Points
MethodDescription
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
MethodDescription
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
MethodDescription
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
MethodDescription
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
MethodDescription
toLowerCase()/toUpperCase()Convert case
trim()/strip()Remove leading/trailing whitespace
🛠️ Replacement & Modification
MethodDescription
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
MethodDescription
split(String regex)Split into array
split(String regex, int limit)Split with limit
lines()Stream of lines (Java 11+)
🔗 Conversion & Encoding
MethodDescription
toCharArray()Convert to char array
getBytes()/getBytes(Charset)Get bytes
intern()Return string from pool
📊 Streaming (Java 8+)
MethodDescription
chars()Stream of UTF‑16 code units
codePoints()Stream of Unicode code points
🎨 Formatting & Joining
MethodDescription
format(String fmt, Object…)Static printf‑style formatting
formatted(Object…)Instance formatting (Java 15+)
join(CharSequence, CharSequence…)Join with delimiter
🧰 Static Utilities
MethodDescription
valueOf(Object obj)Convert any value to String
copyValueOf(char[] data)Convert char array to String

Inspection & Info

MethodDescription
int length()Returns number of characters
boolean isEmpty()Returns true if length == 0

Character Access & Code Points

MethodDescription
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

MethodDescription
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)Region comparison

Searching

MethodDescription
boolean contains(CharSequence s)Checks if substring exists
int indexOf(String str)First occurrence of substring
int lastIndexOf(String str)Last occurrence of substring
boolean startsWith(String prefix)Checks prefix
boolean endsWith(String suffix)Checks suffix

Extraction & Substrings

MethodDescription
String substring(int beginIndex)From beginIndex to end
String substring(int beginIndex, int endIndex)Between begin (inclusive) and end (exclusive)
CharSequence subSequence(int start, int end)Returns same as substring as a CharSequence

Case Conversion & Trimming

MethodDescription
String toLowerCase()All chars to lower case
String toUpperCase()All chars to upper case
String trim()Remove leading/trailing whitespace
String strip() (Java 11+)Unicode-aware trim

Replacement & Modification

MethodDescription
String concat(String str)Append given string
String replace(char oldChar, char newChar)Replace all occurrences of one char
String replaceAll(String regex, String replacement)Replace via regex
String replaceFirst(String regex, String replacement)Replace first match via regex
String repeat(int count) (Java 11+)Repeat this string

Splitting & Lines

MethodDescription
String[] split(String regex)Split by regex
String[] split(String regex, int limit)Split with max parts
Stream<String> lines() (Java 11+)Stream over lines

Conversion & Encoding

MethodDescription
char[] toCharArray()Chars → array
byte[] getBytes()Default-charset bytes
String intern()Canonical representation from pool

Streaming (Java 8+)

MethodDescription
IntStream chars()Stream of UTF-16 code units
IntStream codePoints()Stream of Unicode code points

Formatting & Joining

MethodDescription
static String format(String fmt, Object… args)printf-style formatting
String formatted(Object… args) (Java 15+)Instance formatting
static String join(CharSequence delim, CharSequence… elems)Join with delimiter

Static Utilities

MethodDescription
static String valueOf(Object obj)Any value → String
static String copyValueOf(char[] data)char[]String

Usage tip: Strings are immutable—most methods return a new String, so you can chain calls:

String s = original.trim()
                   .toLowerCase()
                   .replace("foo", "bar")
                   .substring(0, 10);

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *