Java String Class – Methods, Use & Most‑Used Ones

Package: java.lang
public final class String implements Serializable, Comparable, CharSequence

Key fact: String is immutable — every method that “modifies” a string returns a new String object, the original never changes.

1️⃣ Basic Information & Access Methods

int length()

Use: Get total number of characters
Common use: input validation, loops

String s = "Hello";
System.out.println(s.length()); // 5

char charAt(int index)
Use: Access a character at a specific index

System.out.println("Java".charAt(2)); // 'v'

boolean isEmpty()
Use: Check if string length is zero

"".isEmpty(); // true

boolean isBlank() (Java 11+)
Use: True for empty or whitespace‑only strings

"   ".isBlank(); // true

2️⃣ Comparison Methods (Very Important)

boolean equals(Object obj)
Use: Compare content (NOT reference)

"Java".equals("Java"); // true

boolean equalsIgnoreCase(String s)
Use: Case‑insensitive comparison

"Java".equalsIgnoreCase("java"); // true

int compareTo(String s)
Use: Lexicographical comparison (sorting)

"apple".compareTo("banana"); // negative

int compareToIgnoreCase(String s)
Use: Sorting ignoring case





3️⃣ Searching Methods

boolean contains(CharSequence s)
Use: Check substring presence

"Java Developer".contains("Dev"); // true

int indexOf(String s)
Use: First occurrence position

"hello".indexOf("l"); // 2

int lastIndexOf(String s)
Use: Last occurrence position

"hello".lastIndexOf("l"); // 3

boolean startsWith(String prefix)
boolean endsWith(String suffix)
Use: Prefix/suffix checks

"file.txt".endsWith(".txt"); // true
4️⃣ Substring & Extraction

String substring(int beginIndex)
String substring(int beginIndex, int endIndex)
Use: Extract part of string

"HelloWorld".substring(5);      // World
"HelloWorld".substring(0, 5);   // Hello

CharSequence subSequence(int start, int end)
Use: Similar to substring, returns CharSequence

5️⃣ Modification (Returns New String)

String toLowerCase()
String toUpperCase()
Use: Case normalization

"Java".toUpperCase(); // JAVA

String trim()
Use: Remove leading & trailing spaces

"  hi  ".trim(); // "hi"

String strip() (Java 11+)
Use: Unicode‑aware trim (better than trim)

String replace(char old, char new)
String replace(CharSequence old, CharSequence new)
Use: Replace characters or substrings

"banana".replace("a", "o"); // bonono

String replaceAll(String regex, String replacement)
String replaceFirst(String regex, String replacement)
Use: Regex‑based replacements

6️⃣ Splitting & Joining

String[] split(String regex)
Use: Tokenize string

"one,two,three".split(",");

static String join(CharSequence delimiter, CharSequence… elements)
Use: Join strings

String.join("-", "2026", "04", "05"); // 2026-04-05

7️⃣ Conversion Methods

char[] toCharArray()
Use: Character‑level processing

byte[] getBytes()
Use: Encoding / I/O operations

static String valueOf(anyType)
Use: Convert primitives/objects to string

String.valueOf(123); // "123"

8️⃣ Unicode‑Aware Methods (Advanced)

int codePointAt(int index)
int codePointCount(int begin, int end)
Use: Emoji / surrogate‑pair safe processing

✅ MOST COMMONLY USED STRING METHODS (Interview + Projects)

Used daily (90% of code)

length()
isEmpty(), isBlank()
equals(), equalsIgnoreCase()
contains()
substring()
split()
replace()
toUpperCase(), toLowerCase()
trim() / strip()
indexOf(), lastIndexOf()

Used often in backend / automation

startsWith(), endsWith()
compareTo()
join()
toCharArray()
valueOf()

Advanced / Unicode handling

codePointAt()
codePointCount()

 

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment