Java String Methods: Complete Categorized Reference Guide

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.

1. Inspection, Sizing & Character Lookup 🔝

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).

2. Equality, Sorting & Hash Codes 🔝

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.

3. Substrings, Slicing & Array Extraction 🔝

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.

4. Search, Affixes & Pattern Matching 🔝

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.

5. Text Transformation, Replacement & Formatting 🔝

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.

6. Joining, Value Conversion & Memory Pooling 🔝

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.

7. 32-Bit Unicode Code Point Processing 🔝

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).

8. Fundamental Principle: String Immutability in Java 🔝

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);  // HELLO

Terminal Output:

After method call: Hello
After reassignment: HELLO

9. Master Alphabetical Directory (All 34 Methods) 🔝

Complete A-to-Z index of every individual Java String method tutorial:

Method Category Direct Tutorial Link
charAt(index)InspectioncharAt() Tutorial
codePointAt(index)UnicodecodePointAt() Tutorial
codePointBefore(index)UnicodecodePointBefore() Tutorial
codePointCount(b, e)UnicodecodePointCount() Tutorial
compareTo(str)ComparisoncompareTo() Tutorial
compareToIgnoreCase(str)ComparisoncompareToIgnoreCase() Tutorial
concat(str)Joiningconcat() Tutorial
contains(seq)Matchingcontains() Tutorial
copyValueOf(data)ConversioncopyValueOf() Tutorial
endsWith(suffix)MatchingendsWith() Tutorial
equals(obj)Equalityequals() Tutorial
equalsIgnoreCase(str)EqualityequalsIgnoreCase() Tutorial
format(fmt, args)Modificationformat() Tutorial
getBytes()ExtractiongetBytes() Tutorial
getChars(...)ExtractiongetChars() Tutorial
hashCode()EqualityhashCode() Tutorial
indexOf(...)InspectionindexOf() Tutorial
intern()Conversionintern() Tutorial
isEmpty()InspectionisEmpty() Tutorial
join(...)Joiningjoin() Tutorial
lastIndexOf(...)InspectionlastIndexOf() Tutorial
length()Inspectionlength() Tutorial
matches(regex)Matchingmatches() Tutorial
replace(target, repl)Modificationreplace() Tutorial
replaceAll(regex, repl)ModificationreplaceAll() Tutorial
replaceFirst(regex, repl)ModificationreplaceFirst() Tutorial
split(regex)Extractionsplit() Tutorial
startsWith(prefix)MatchingstartsWith() Tutorial
substring(b, e)Extractionsubstring() Tutorial
toCharArray()ExtractiontoCharArray() Tutorial
toLowerCase()ModificationtoLowerCase() Tutorial
toUpperCase()ModificationtoUpperCase() Tutorial
trim()Modificationtrim() Tutorial
valueOf(val)ConversionvalueOf() Tutorial

Runnable Summary Example 🔝

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!

10. Frequently Asked Questions (FAQ) 🔝

What is the difference between String, StringBuilder, and StringBuffer?

String is immutable. StringBuilder is mutable and unsynchronized (high performance for single threads). StringBuffer is mutable and thread-safe (synchronized).

Why should I use equals() instead of == when comparing strings?

The == operator compares object references (memory addresses), whereas equals() compares character values.

What is the JVM String Constant Pool?

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().

How do I test if a String is empty or blank?

Use isEmpty() to check if length is 0. To check if a string contains only whitespace, combine with trim(): str.trim().isEmpty().


Java



plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer