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)
Contains information related to marketing campaigns of the user. These are shared with Google AdWords / Google Ads when the Google Ads and Google Analytics accounts are linked together.
90 days
__utma
ID used to identify users and sessions
2 years after last activity
__utmt
Used to monitor number of Google Analytics server requests
10 minutes
__utmb
Used to distinguish new sessions and visits. This cookie is set when the GA.js javascript library is loaded and there is no existing __utmb cookie. The cookie is updated every time data is sent to the Google Analytics server.
30 minutes after last activity
__utmc
Used only with old Urchin versions of Google Analytics and not with GA.js. Was used to distinguish between new sessions and visits at the end of a session.
End of session (browser)
__utmz
Contains information about the traffic source or campaign that directed user to the website. The cookie is set when the GA.js javascript is loaded and updated when data is sent to the Google Anaytics server
6 months after last activity
__utmv
Contains custom information set by the web developer via the _setCustomVar method in Google Analytics. This cookie is updated every time new data is sent to the Google Analytics server.
2 years after last activity
__utmx
Used to determine whether a user is included in an A / B or Multivariate test.
18 months
_ga
ID used to identify users
2 years
_gali
Used by Google Analytics to determine which links on a page are being clicked
30 seconds
_ga_
ID used to identify users
2 years
_gid
ID used to identify users for 24 hours after last activity
24 hours
_gat
Used to monitor number of Google Analytics server requests when using Google Tag Manager
Leave a Reply