The java.lang.String class represents immutable character sequences in Java, serving as the core foundation for text processing.
Categorized Master Directory: Because Java strings are immutable, methods return new string instances rather than altering original data. This hub organizes all 34 core methods into logical functional clusters: inspection, equality, slicing, regex matching, text modification, conversion, and 32-bit Unicode code point decoding.
Methods for determining string length, emptiness, and locating character coordinates:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
length() |
int |
Returns the total count of 16-bit UTF-16 code units in the string. |
isEmpty() |
boolean |
Returns true if and only if length() is 0. |
charAt(int index) |
char |
Returns the char value at the specified 0-based index. |
indexOf(...) |
int |
Returns the index of the first occurrence of a character or substring (or -1). |
lastIndexOf(...) |
int |
Returns the index of the last occurrence of a character or substring (or -1). |
Comparing text contents, lexicographical ordering, and calculating hash codes:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
equals(Object anObject) |
boolean |
Compares this string to another object for character-by-character value equality. |
equalsIgnoreCase(String another) |
boolean |
Compares two strings for content equality, ignoring lowercase/uppercase differences. |
compareTo(String another) |
int |
Compares two strings lexicographically based on Unicode character values. |
compareToIgnoreCase(String str) |
int |
Compares two strings lexicographically, ignoring case differences. |
hashCode() |
int |
Returns a signed 32-bit integer hash code computed via polynomial arithmetic. |
Extracting substring segments, splitting tokens, and exporting character or byte buffers:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
substring(int begin, int end) |
String |
Returns a new string representing the substring in range [begin, end). |
split(String regex) |
String[] |
Splits the string into an array of substrings around matches of the regex. |
toCharArray() |
char[] |
Allocates a fresh character array copy of the string's characters. |
getChars(...) |
void |
Copies characters in-place into an existing, pre-allocated destination char[]. |
getBytes(...) |
byte[] |
Encodes the string into a sequence of binary bytes using a specified Charset. |
Testing prefixes, suffixes, contained substrings, and full regular expression matches:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
contains(CharSequence s) |
boolean |
Returns true if and only if this string contains the specified character sequence. |
startsWith(String prefix) |
boolean |
Tests if this string starts with the specified prefix sequence. |
endsWith(String suffix) |
boolean |
Tests if this string ends with the specified suffix sequence. |
matches(String regex) |
boolean |
Tells whether or not this full string matches the given regular expression pattern. |
Case changes, whitespace trimming, regex substitutions, and printf formatting:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
replace(...) |
String |
Replaces all occurrences of a literal character or substring without regex parsing. |
replaceAll(...) |
String |
Replaces all substrings matching a regular expression with a replacement string. |
replaceFirst(...) |
String |
Replaces only the first substring matching a regular expression. |
trim() |
String |
Returns a string with leading and trailing ASCII whitespace omitted. |
toLowerCase() |
String |
Converts all characters in this string to lowercase using default or given locale. |
toUpperCase() |
String |
Converts all characters in this string to uppercase using default or given locale. |
format(...) |
String |
Produces formatted text using printf-style template format strings and arguments. |
Joining sequences, converting primitives, and interacting with the String Constant Pool:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
concat(String str) |
String |
Concatenates the specified string to the end of this string. |
join(...) |
String |
Joins an array or iterable collection of elements with a delimiter. |
valueOf(...) |
String |
Returns the string representation of any primitive value, array, or object. |
copyValueOf(...) |
String |
Equivalent to valueOf(char[]); constructs a string from a character array. |
intern() |
String |
Returns the canonical reference from the JVM String Constant Pool. |
Handling supplementary characters, emojis, and multi-byte UTF-16 surrogate pairs:
| Method | Return Type | Description & Operational Contract |
|---|---|---|
codePointAt(int index) |
int |
Returns the 32-bit Unicode code point at the given index, merging surrogate pairs. |
codePointBefore(int index) |
int |
Returns the 32-bit Unicode code point immediately preceding the specified index. |
codePointCount(int b, int e) |
int |
Returns the true count of 32-bit Unicode code points in text range [b, e). |
In Java, String objects are strictly immutable. Once created, their internal character contents can never be changed. Calling methods like toUpperCase(), replace(), or trim() always allocates and returns a new String object:
String s1 = "Hello";
// Calling toUpperCase() DOES NOT mutate s1!
s1.toUpperCase();
System.out.println("After method call: " + s1); // Hello (unchanged)
// Correct approach: assign the returned reference
s1 = s1.toUpperCase();
System.out.println("After reassignment: " + s1); // HELLOTerminal Output:
After method call: Hello
After reassignment: HELLO
Complete A-to-Z index of every individual Java String method tutorial:
| Method | Category | Direct Tutorial Link |
|---|---|---|
charAt(index) | Inspection | charAt() Tutorial |
codePointAt(index) | Unicode | codePointAt() Tutorial |
codePointBefore(index) | Unicode | codePointBefore() Tutorial |
codePointCount(b, e) | Unicode | codePointCount() Tutorial |
compareTo(str) | Comparison | compareTo() Tutorial |
compareToIgnoreCase(str) | Comparison | compareToIgnoreCase() Tutorial |
concat(str) | Joining | concat() Tutorial |
contains(seq) | Matching | contains() Tutorial |
copyValueOf(data) | Conversion | copyValueOf() Tutorial |
endsWith(suffix) | Matching | endsWith() Tutorial |
equals(obj) | Equality | equals() Tutorial |
equalsIgnoreCase(str) | Equality | equalsIgnoreCase() Tutorial |
format(fmt, args) | Modification | format() Tutorial |
getBytes() | Extraction | getBytes() Tutorial |
getChars(...) | Extraction | getChars() Tutorial |
hashCode() | Equality | hashCode() Tutorial |
indexOf(...) | Inspection | indexOf() Tutorial |
intern() | Conversion | intern() Tutorial |
isEmpty() | Inspection | isEmpty() Tutorial |
join(...) | Joining | join() Tutorial |
lastIndexOf(...) | Inspection | lastIndexOf() Tutorial |
length() | Inspection | length() Tutorial |
matches(regex) | Matching | matches() Tutorial |
replace(target, repl) | Modification | replace() Tutorial |
replaceAll(regex, repl) | Modification | replaceAll() Tutorial |
replaceFirst(regex, repl) | Modification | replaceFirst() Tutorial |
split(regex) | Extraction | split() Tutorial |
startsWith(prefix) | Matching | startsWith() Tutorial |
substring(b, e) | Extraction | substring() Tutorial |
toCharArray() | Extraction | toCharArray() Tutorial |
toLowerCase() | Modification | toLowerCase() Tutorial |
toUpperCase() | Modification | toUpperCase() Tutorial |
trim() | Modification | trim() Tutorial |
valueOf(val) | Conversion | valueOf() Tutorial |
Demonstrating multiple string operations working in tandem:
String sample = " Welcome to plus2net Java Tutorials! ";
// 1. Cleaning and inspection
String clean = sample.trim();
System.out.println("Cleaned Length: " + clean.length()); // 35
System.out.println("Starts with: " + clean.startsWith("Wel")); // true
// 2. Case transformation and slicing
String upper = clean.toUpperCase();
String topic = clean.substring(11, 19);
System.out.println("Topic slice: [" + topic + "]"); // [plus2net]
// 3. Delimited joining and regex replacement
String replaced = clean.replaceAll("\\s+", "_");
System.out.println("Underscored: " + replaced);Terminal Output:
Cleaned Length: 35
Starts with: true
Topic slice: [plus2net]
Underscored: Welcome_to_plus2net_Java_Tutorials!
String is immutable. StringBuilder is mutable and unsynchronized (high performance for single threads). StringBuffer is mutable and thread-safe (synchronized).
The == operator compares object references (memory addresses), whereas equals() compares character values.
It is a dedicated memory space within the Java heap where unique string literals are cached to reduce memory consumption. Strings can be added dynamically using intern().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.