String Indexing
What is String Indexing?
String indexing refers to the position number of each character in a
string.
In Java, indexing always starts from 0.
Example
String s = "COMPUTER";
Character : C O M P U T E R
Index : 0 1 2 3 4 5 6 7
Important Points
Indexing starts from 0.
The first character is always at index 0.
The last character is always at index (length - 1).
Spaces are also counted as characters and have an index.
Index values are used in methods like charAt(), substring(),
and indexOf().
Example 1
String s = "Java";
[Link]([Link](0));
Output:
J
1
Page
Explanation:
J a v a
0 1 2 3
Character at index 0 is J.
Example 2
String s = "Java";
[Link]([Link](3));
Output:
a
Explanation:
J a v a
0 1 2 3
Character at index 3 is a.
Example 3
String s = "Hello World";
[Link]([Link]());
Output:
11
Explanation:
H e l l o _ W o r l d
0 1 2 3 4 5 6 7 8 9 10
The space is also counted as a character.
2
Page
Formula
Last Index = Length - 1
Example:
String s = "COMPUTER";
Length = 8
Last Index = 8 - 1 = 7
Common Mistake
String s = "Java";
[Link]([Link](4));
Output:
StringIndexOutOfBoundsException
Reason:
Valid indexes are:
0, 1, 2, 3
Index 4 does not exist.
3
Page
Different Methods and Their Output in
String
1. length()
Purpose
Returns the total number of characters present in a string.
Return Type
int
Syntax
[Link]();
Example 1
String s = "Java";
[Link]([Link]());
Output:
4
Example 2
String s = "COMPUTER";
[Link]([Link]());
Output:
8
Example 3
String s = "Hello World";
4
Page
[Link]([Link]());
Output:
11
Important Points
Counts spaces as characters.
Returns an integer value.
Length starts from 1, but indexing starts from 0.
Exception
String s = null;
[Link]([Link]());
Output:
NullPointerException
2. charAt(int index)
Purpose
Returns the character present at the specified index.
Return Type
char
Syntax
[Link](index);
Example 1
String s = "Java";
[Link]([Link](0));
5
Output:
Page
J
Example 2
String s = "Computer";
[Link]([Link](3));
Output:
p
Example 3
String s = "India";
[Link]([Link](4));
Output:
a
Important Points
Index starts from 0.
Returns only one character.
Return type is char.
Exception
String s = "Java";
[Link]([Link](4));
Output:
StringIndexOutOfBoundsException
6
Page
3. indexOf(char ch)
Purpose
Returns the index of the first occurrence of the specified
character in the string.
Return Type
int
Syntax
[Link](ch);
Example 1
String s = "Java";
[Link]([Link]('a'));
Output:
1
Example 2
String s = "banana";
[Link]([Link]('a'));
Output:
1
Example 3
String s = "computer";
[Link]([Link]('m'));
Output:
2
7
Page
Example 4
String s = "school";
[Link]([Link]('o'));
Output:
3
Character Not Found
String s = "Java";
[Link]([Link]('z'));
Output:
-1
Important Points
Returns the first occurrence only.
Returns -1 if character is not found.
Case-sensitive method.
Example:
String s = "Java";
[Link]([Link]('J'));
[Link]([Link]('j'));
Output:
0
-1
8
Page
4. indexOf(char ch, int startIndex)
Purpose
Searches for a character starting from the specified index
and returns its first occurrence after that index.
Return Type
int
Syntax
[Link](ch,startIndex);
Example 1
String s = "banana";
[Link]([Link]('a',2));
Output:
3
Example 2
String s = "Java";
[Link]([Link]('a',2));
Output:
3
Example 3
String s = "Mississippi";
[Link]([Link]('s',3));
Output:
3
9
Page
Example 4
String s = "Mississippi";
[Link]([Link]('s',4));
Output:
5
Character Not Found
String s = "Java";
[Link]([Link]('A',2));
Output:
-1
Important Points
Search starts from the given index.
Returns -1 if character is not found.
Case-sensitive method.
String s = "banana";
[Link]([Link]('a',2));
Output:
3
Not 1, because searching starts from index 2.
Exception
No exception occurs if character is absent.
10
Page
It simply returns:
-1
5. lastIndexOf(char ch)
Purpose
Returns the index of the last occurrence of the specified
character in the string.
Return Type
int
Syntax
[Link](ch);
Example 1
String s = "Java";
[Link]([Link]('a'));
Output:
3
Example 2
String s = "banana";
[Link]([Link]('a'));
Output:
5
Example 3
String s = "Mississippi";
[Link]([Link]('s'));
11
Output:
Page
6
Example 4
String s = "computer";
[Link]([Link]('o'));
Output:
1
Character Not Found
String s = "Java";
[Link]([Link]('z'));
Output:
-1
Important Points
Returns the last occurrence only.
Returns -1 if character is not found.
Case-sensitive method.
Example:
String s = "Java";
[Link]([Link]('a'));
[Link]([Link]('A'));
Output:
3
-1
12
Page
6. equals(String str)
Purpose
Compares two strings to check whether they are exactly
identical or not.
Return Type
boolean
Syntax
[Link](string2);
Example 1
String a = "Java";
String b = "Java";
[Link]([Link](b));
Output:
true
Example 2
String a = "Java";
String b = "JAVA";
[Link]([Link](b));
Output:
false
Example 3
String a = "Computer";
String b = "Laptop";
13
[Link]([Link](b));
Page
Output:
false
Example 4
String a = "";
String b = "";
[Link]([Link](b));
Output:
true
Important Points
Return type is boolean.
Case-sensitive method.
Compares contents of strings.
Returns only true or false.
Difference Between == and equals()
String a = new String("Java");
String b = new String("Java");
[Link](a == b);
[Link]([Link](b));
Output:
false
true
Explanation:
== → compares memory addresses
14
equals() → compares contents
Page
Exception
String a = null;
[Link]([Link]("Java"));
Output:
NullPointerException
7. equalsIgnoreCase(String str)
Purpose
Compares two strings after ignoring case differences.
Return Type
boolean
Syntax
[Link](string2);
Example 1
[Link]("Java".equalsIgnoreCase("JAVA"))
;
Output:
true
Example 2
[Link]("COMPUTER".equalsIgnoreCase("com
puter"));
Output:
15
true
Page
Example 3
[Link]("India".equalsIgnoreCase("INDIA"
));
Output:
true
Example 4
[Link]("Java".equalsIgnoreCase("Python"
));
Output:
false
Example 5
[Link]("School".equalsIgnoreCase("sChOo
L"));
Output:
true
Important Points
Return type is boolean.
Ignores uppercase and lowercase differences.
Compares string contents.
Returns only true or false.
Common Exam Question
String x = "COMPUTER";
String y = "computer";
[Link]([Link](y));
16
Page
Output:
true
Difference Between equals() and equalsIgnoreCase()
Method Case Sensitive
equals() Yes
equalsIgnoreCase() No
Example:
[Link]("Java".equals("JAVA"));
[Link]("Java".equalsIgnoreCase("JAVA"))
;
Output:
false
true
Exception
String a = null;
[Link]([Link]("Java"));
Output:
NullPointerException
Important for 2027
8. compareTo(String str)
Purpose
Compares two strings lexicographically (dictionary order)
17
using Unicode values.
Page
Return Type
int
Syntax
[Link](string2);
Rules
Result Meaning
0 Both strings are equal
Positive First string is greater
Negative First string is smaller
Example 1
[Link]("apple".compareTo("apple"));
Output:
0
Example 2
[Link]("cat".compareTo("bat"));
Output:
1
Because:
'c' = 99
'b' = 98
99 - 98 = 1
Example 3
[Link]("apple".compareTo("ball"));
18
Output:
Page
-1
Because:
97 - 98 = -1
Example 4 (Prefix Case)
[Link]("KING".compareTo("KINGDOM"));
Output:
-3
Because:
length("KING") = 4
length("KINGDOM") = 7
4 - 7 = -3
Example 5
[Link]("KINGDOM".compareTo("KING"));
Output:
3
Because:
7 - 4 = 3
Example 6
[Link]("Hello".compareTo("hello"));
Output:
-32
Because:
19
72 - 104 = -32
Page
Important Points
Return type is int.
Uses Unicode values.
Case-sensitive.
First unequal character determines the result.
If one string is a prefix of another, length difference is
returned.
Exception
String s = null;
[Link]([Link]("Java"));
Output:
NullPointerException
9. compareToIgnoreCase(String str)
Purpose
Compares two strings lexicographically after ignoring case
differences.
Return Type
int
Syntax
[Link](string2);
20
Page
Rules
Result Meaning
0 Both strings are equal
Positive First string is greater
Negative First string is smaller
Example 1
[Link]("Apple".compareToIgnoreCase("app
le"));
Output:
0
Explanation:
Case is ignored.
APPLE = APPLE
Example 2
[Link]("A".compareToIgnoreCase("b"));
Output:
-1
Explanation:
a = 97
b = 98
97 - 98 = -1
21
Page
Example 3
[Link]("Zebra".compareToIgnoreCase("app
le"));
Output:
25
Explanation:
z = 122
a = 97
122 - 97 = 25
Example 4
[Link]("KING".compareToIgnoreCase("king
"));
Output:
0
Example 5 (Prefix Case)
[Link]("KING".compareToIgnoreCase("KING
DOM"));
Output:
-3
Explanation:
4 - 7 = -3
22
Page
Important Points
Return type is int.
Ignores uppercase and lowercase differences.
Uses Unicode values.
If one string is a prefix of another, length difference is
returned.
Similar to compareTo(), but case is ignored.
Difference Between compareTo() and compareToIgnoreCase()
Method Case Sensitive
compareTo() Yes
compareToIgnoreCase() No
Example:
[Link]("Java".compareTo("JAVA"));
[Link]("Java".compareToIgnoreCase("JAVA
"));
Output:
32
0
Exception
String s = null;
[Link]([Link]("Java"));
Output:
NullPointerException
23
Page
Important for 2027
10. substring(int beginIndex)
Purpose
Returns a new string starting from the specified index up to
the end of the string.
Return Type
String
Syntax
[Link](beginIndex);
Example 1
[Link]("computer".substring(3));
Output:
puter
Example 2
[Link]("hello".substring(1));
Output:
ello
Example 3
[Link]("india".substring(2));
24
Page
Output:
dia
Example 4
[Link]("Java".substring(0));
Output:
Java
Example 5
[Link]("PROGRAM".substring(4));
Output:
RAM
Important Points
Return type is String.
Index starts from 0.
Characters before the specified index are removed.
Original string remains unchanged.
Common Exam Question
String s = "COMPUTER";
[Link]([Link](4));
Output:
UTER
25
Page
Exception
[Link]("Java".substring(5));
Output:
StringIndexOutOfBoundsException
Special Case
[Link]("Java".substring(4));
Output:
(Empty String)
11. substring(int beginIndex, int endIndex)
Purpose
Returns a part of the string from beginIndex to endIndex -
1.
Return Type
String
Syntax
[Link](beginIndex,endIndex);
Formula
beginIndex included
endIndex excluded
26
Page
Example 1
[Link]("computer".substring(1,4));
Output:
omp
Example 2
[Link]("hello".substring(0,2));
Output:
he
Example 3
[Link]("india".substring(2,4));
Output:
di
Example 4
[Link]("Java".substring(1,3));
Output:
av
Example 5
[Link]("PROGRAM".substring(2,6));
Output:
27
Page
OGRA
Important Points
Return type is String.
beginIndex is included.
endIndex is excluded.
Original string remains unchanged.
Understanding the Rule
For:
COMPUTER
01234567
[Link]("COMPUTER".substring(2,6));
Output:
MPUT
Because:
2 included
6 excluded
Characters taken:
2,3,4,5
Common Exam Question
[Link]("Java".substring(0,2));
Output:
28
Page
Ja
Special Case
[Link]("Java".substring(2,2));
Output:
(Empty String)
Exception 1
[Link]("Java".substring(2,7));
Output:
StringIndexOutOfBoundsException
Exception 2
[Link]("Java".substring(3,1));
Output:
StringIndexOutOfBoundsException
12. trim()
Purpose
Removes leading (beginning) and trailing (ending) spaces from a
29
string.
Page
Return Type
String
Syntax
[Link]();
Example 1
String s = " Java ";
[Link]([Link]());
Output:
Java
Example 2
String s = " COMPUTER";
[Link]([Link]());
Output:
COMPUTER
Example 3
String s = "ICSE ";
[Link]([Link]());
Output:
ICSE
Important Points
Removes spaces only from the beginning and end.
Spaces in the middle remain unchanged.
String s = "Java Programming";
[Link]([Link]());
Output:
Java Programming
30
Page
13. toUpperCase()
Purpose
Converts all lowercase letters into uppercase letters.
Return Type
String
Syntax
[Link]();
Example 1
String s = "java";
[Link]([Link]());
Output:
JAVA
Example 2
String s = "Java123";
[Link]([Link]());
Output:
JAVA123
Example 3
String s = "java@2025";
[Link]([Link]());
Output:
JAVA@2025
Important Points
Converts only letters.
Digits remain unchanged.
31
Special characters remain unchanged.
Page
Original string remains unchanged.
14. toLowerCase()
Purpose
Converts all uppercase letters into lowercase letters.
Return Type
String
Syntax
[Link]();
Example 1
String s = "JAVA";
[Link]([Link]());
Output:
java
Example 2
String s = "JAVA123";
[Link]([Link]());
Output:
java123
Example 3
String s = "JAVA@2025";
[Link]([Link]());
Output:
java@2025
Important Points
32
Converts only letters.
Page
Digits remain unchanged.
Special characters remain unchanged.
Original string remains unchanged.
15. concat(String str)
Purpose
Joins one string with another string.
Return Type
String
Syntax
[Link](string2);
Example 1
String s1 = "Hello";
String s2 = "World";
[Link]([Link](s2));
Output:
HelloWorld
Example 2
String s1 = "ICSE ";
String s2 = "Class X";
[Link]([Link](s2));
Output:
ICSE Class X
Example 3
33
String s = "Java";
Page
[Link]([Link](" Programming"));
Output:
Java Programming
Important Points
Similar to + operator.
Return type is String.
Original string remains unchanged.
16. startsWith(String prefix)
Purpose
Checks whether a string starts with the specified sequence.
Return Type
boolean
Syntax
[Link](prefix);
Example 1
String s = "Computer";
[Link]([Link]("Com"));
Output:
true
Example 2
String s = "Computer";
[Link]([Link]("com"));
34
Page
Output:
false
Example 3
String s = "Programming";
[Link]([Link]("Pro"));
Output:
true
Important Points
Case-sensitive.
Return type is boolean.
17. endsWith(String suffix)
Purpose
Checks whether a string ends with the specified sequence.
Return Type
boolean
Syntax
[Link](suffix);
Example 1
String s = "Computer";
[Link]([Link]("ter"));
Output:
true
35
Example 2
String s = "Computer";
Page
[Link]([Link]("TER"));
Output:
false
Example 3
String s = "Programming";
[Link]([Link]("ing"));
Output:
true
Important Points
Case-sensitive.
Return type is boolean.
18. replace(char oldChar, char newChar)
Purpose
Replaces all occurrences of a character with another character.
Return Type
String
Syntax
[Link](oldChar,newChar);
Example 1
String s = "banana";
[Link]([Link]('a','o'));
36
Output:
Page
bonono
Example 2
String s = "school";
[Link]([Link]('o','a'));
Output:
schaal
Example 3
String s = "Java";
[Link]([Link]('a','A'));
Output:
JAvA
Important Points
Replaces all occurrences.
Original string remains unchanged.
Return type is String.
19. [Link]()
Purpose
Converts primitive data types into String format.
Return Type
String
Syntax
[Link](value);
37
Page
Example 1 (int)
int x = 100;
String s = [Link](x);
[Link](s);
Output:
100
Example 2 (double)
double d = 45.67;
String s = [Link](d);
[Link](s);
Output:
45.67
Example 3 (char)
char ch = 'A';
String s = [Link](ch);
[Link](s);
Output:
A
Example 4 (boolean)
boolean b = true;
String s = [Link](b);
[Link](s);
Output:
38
true
Page
Example 5 (Expression)
int a = 10;
int b = 20;
String s = [Link](a + b);
[Link](s);
Output:
30
Explanation
Java first calculates:
a + b
Result:
30
Then converts it into String:
[Link](30)
Result:
"30"
Example 6
int a = 10;
int b = 20;
String s = [Link](a) + [Link](b);
[Link](s);
Output:
1020
39
Explanation
Page
[Link](10) = "10"
[Link](20) = "20"
"10" + "20" = "1020"
Important Points
valueOf() is a static method.
Called using: [Link]()
Return type is always String.
Frequently used to convert numbers into strings before
applying String methods.
40
Page