Method Description Syntax Example
Returns number of
length() characters in the str.length() "Hello".length() → 5
string.
Returns character at
charAt(int index) the given index (0- str.charAt(1) "Java".charAt(2) → 'v'
based).
Finds first index of
indexOf(char ch) str.indexOf('a') "banana".indexOf('a')
given character.
Finds index of
indexOf(char ch, int "banana".indexOf('a', 2
character from given str.indexOf('a', 2)
startIndex) 3
index.
Finds last occurrence str.lastIndexOf('a' "banana".lastIndexOf('
lastIndexOf(char ch)
index of character. ) 5
Extracts substring
substring(int "Welcome".substring(3
from start index to str.substring(3)
startIndex) "come"
end.
substring(int start, int Extracts substring "Welcome".substring(1
str.substring(1, 4)
end) from start to (end - 1). "elc"
Converts all letters to "ICSE".toLowerCase() →
toLowerCase() str.toLowerCase()
lowercase. "icse"
Converts all letters to "java".toUpperCase() →
toUpperCase() str.toUpperCase()
uppercase. "JAVA"
Replaces all old
replace(char old, char "banana".replace('a','e
characters with new str.replace('a','e')
new) "benene"
ones.
replace(CharSequence Replaces all old
str.replace("is", "This is".replace("is", "
oldSeq, CharSequence substrings with new
"was") → "Thwas was"
newSeq) ones.
Joins the given string str.concat(" "Hello".concat(" World
concat(String s)
to the end. World") "Hello World"
Checks if two strings
"Java".equals("java") →
equals(String s) are exactly the same str.equals("Java")
false
(case-sensitive).
Method Description Syntax Example
equalsIgnoreCase(Stri Checks string equality str.equalsIgnoreC "Java".equalsIgnoreCa
ng s) ignoring case. ase("java") va") → true
Lexicographically str.compareTo("a "banana".compareTo("
compareTo(String s)
compares strings. pple") ") → 1
Lexicographic
compareToIgnoreCase( str.compareToIgn "apple".compareToIgno
compare ignoring
String s) oreCase("Apple") se("Apple") → 0
case.
Removes spaces from
trim() str.trim() " Java ".trim() → "Java"
start and end.
Checks if string ends str.endsWith("en "weekend".endsWith("
endsWith(String suffix)
with the given suffix. d") → true
String.valueOf(primitiv Converts primitive to String.valueOf(12
String.valueOf(123) →
e) string. 3)
parseXxx(String s) e.g. Converts string to Integer.parseInt(" Integer.parseInt("456"
Integer.parseInt(s) primitive type. 456") 456
✅ 1. Introduction to Strings
A String in Java is a sequence of characters.
Strings are objects of the String class in java.lang package.
They are immutable: Once created, their values cannot be
changed.
✅ 2. Declaring and Creating Strings
Ways to create strings:
1. String literal
String name = "ICSE";
2. Using new keyword
String name = new String("ICSE");
📘 Java String Methods – With Description, Syntax, Example &
Explanation