A String is a sequence of characters — words, sentences, names. Java's String class gives many ready methods to measure, search and transform text. This is a major ICSE Class 10 topic.
1Strings and an array of strings
A String is created with double quotes. Strings are indexed from 0, like arrays:
String s = "Vidaara"; // s.charAt(0) is 'V'
String[] names = {"Asha", "Ravi", "Sara"}; // an array of Strings
for (int i = 0; i < names.length; i++)
System.out.println(names[i]);Strings are immutable — methods like
toUpperCase() return a new String rather than changing the original. Always use the returned value.Key points
- A String is a sequence of characters, indexed from 0 like an array.
- An array of Strings (String[]) holds several words/names.
- Strings are immutable: methods return a NEW string, they don't change the original.
2Core String methods
| Method | Returns |
|---|---|
length() | Number of characters |
charAt(i) | Character at index i |
substring(a, b) | Part from index a up to (not including) b |
indexOf(x) / lastIndexOf(x) | First / last position of x (or -1) |
toUpperCase() / toLowerCase() | Changed-case copy |
trim() | Copy with leading/trailing spaces removed |
concat(t) | This string joined with t |
replace(a, b) | Copy with each a replaced by b |
equals(t) / equalsIgnoreCase(t) | true if equal (optionally ignoring case) |
compareTo(t) | 0 if equal; <0 or >0 by dictionary order |
startsWith(x) / endsWith(x) | true if it begins / ends with x |
String s = "Vidaara"; s.length(); // 7 s.charAt(1); // 'i' s.substring(0, 3); // "Vid" s.toUpperCase(); // "VIDAARA"
Key points
- length() gives the size; charAt(i) the character at i; substring(a,b) a part of the string.
- indexOf finds a position (or -1); equals/equalsIgnoreCase/compareTo compare strings.
- Use equals() (not ==) to compare String contents in Java.
3Scanning characters & classic programs
Many string problems loop through the characters with charAt:
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// process c
}Three classic programs
- Palindrome check — a word that reads the same backwards (MADAM). Compare the string with its reverse, or compare characters from both ends.
- Pig Latin — move the first letter to the end and add "ay": cat → atcay.
- Sort characters alphabetically — put each character in an array and sort it (bubble/selection), then rebuild the string.
For a palindrome, compare
s.charAt(i) with s.charAt(length-1-i) moving inward; if all pairs match, it's a palindrome.Key points
- Loop with charAt(i) from 0 to length()-1 to process each character.
- Palindrome: the string equals its reverse (compare characters from both ends).
- Pig Latin moves the first letter to the end and adds 'ay'; character-sort uses an array + sorting.
★ Practical: string programs
In BlueJ, write programs that:
- Print the length of a word and its first and last characters.
- Count how many vowels a word contains by scanning with charAt.
- Check whether a word is a palindrome.
- Convert a word to Pig Latin (first letter to the end + "ay").
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.