String Handling
ICSE Class 10 · Computer Applications · Chapter 8
What this chapter covers
- The String class
- Length, character access and case methods
- Search methods
- Comparison methods
- Extraction and modification methods
- Extracting and modifying the characters of a string
- String arrays
- Alphabetical order of strings: bubble sort
- Alphabetical order of strings: selection sort
- Searching for a string: linear search
Key points
- A string is a sequence of characters written in double quotes; `String` is a class in `java.lang`, so a String variable holds a reference to an object.
- Characters in a string are indexed from 0 to `length() - 1`; an index outside this range causes `StringIndexOutOfBoundsException`.
- Strings are immutable: every method that seems to change a string returns a new string, which must be stored or used.
- `length()` counts characters; `charAt(n)` returns the character at index `n`; `trim()` removes spaces only at the two ends.
- `toUpperCase()` and `toLowerCase()` change only letters; digits and symbols stay the same.
- `indexOf(ch)` and `lastIndexOf(ch)` return the first and last positions of a character, or -1 if it is absent.
- `startsWith(str)` and `endsWith(str)` return `true` or `false` and are case-sensitive.
- Compare string contents with `equals()` or `equalsIgnoreCase()`, never with `==`.
- `compareTo()` returns 0 for equal strings, the difference of Unicode values at the first mismatch, or the difference of lengths when one string begins the other; `compareToIgnoreCase()` does the same while ignoring case.
- `substring(b, e)` returns characters from index `b` to index `e - 1`, so its length is `e - b`.
- `replace(old, new)` replaces every occurrence of a character; `concat(str)` joins a string; `String.valueOf()` turns any type into a string.
- String programs build a new result string in a loop using `charAt()`: counting characters, toggling case, reversing, palindromes, Pig Latin and initials.
- Bubble sort and selection sort put a String array in alphabetical order by using `compareTo()` in place of `>` or `<`.
- Linear search on a String array compares the key with each element using `equals()` or `equalsIgnoreCase()` and reports -1 if it is not found.
Questions answered
Define the String class and state the package it belongs to.
Why is a Java string said to be immutable?
Give the output of `System.out.println("Ans: " + 2 + 3);` and `System.out.println(2 + 3 + " Ans");`.
Differentiate between `length` and `length()`.
For `s = "HYDERABAD"`, find `s.indexOf('A')`, `s.lastIndexOf('A')` and `s.charAt(s.length() - 2)`.
Why should `equals()` be used instead of `==` to compare two strings?
What value does `"CRICKET".compareTo("CRICKETER")` return? Give the reason.
What value does `"apple".compareTo("Apple")` return?
Past board exam questions
ICSE 2026 board paper · 15 marks
Write a program to accept a word and print the Symbolic of the accepted word. Symbolic word is formed by extracting the characters from the first consonant, then add characters before the first consonant of the accepted word and end with "TR". Example: AIRWAYS Symbolic word is RWAYSAITR BEAUTY Symbolic word is BEAUTYTR
Show answer
ICSE 2026 board paper · 1 mark
To extract the word NOW from the word "ACKNOWLEDGEMENT", the Java statement "ACKNOWLEDGEMENT".substring(3, ____) is used. Choose the correct number to fill in the blank.
- 6
- 7
- 5
- 8
Show answer
ICSE 2026 board paper · 1 mark
Assertion (A): The substring() method modifies the original String. Reason (R): The substring() method can extract part of a String starting from a specific index.
- (A) is true and (R) is false.
- (A) is false and (R) is true.
- Both (A) and (R) are true and (R) is the correct explanation of (A).
- Both (A) and (R) are true, but (R) is not the correct explanation of (A).
Show answer
ICSE 2026 board paper · 1 mark
``` String a[ ] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"}; System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2)); ``` The output of the above statement is:
- da
- d * a
- ti
- t * i
Show answer
ICSE 2026 board paper · 1 mark
Which of the following String methods returns a negative value?
- length()
- equals()
- compareTo()
- charAt()
Show answer
ICSE 2026 board paper · 2 marks
Give the output of the following program segment: ``` String S = "GRACIOUS".substring(4); System.out.println(S); System.out.println ("GLAMOROUS".endsWith(S)); ```
Show answer
ICSE 2026 board paper · 2 marks
Name the following: (a) The return data type of the method equals(). (b) The String method which has no parameter and returns a String.
Show answer
ICSE 2025 board paper · 15 marks
Define a class to accept a String and Print if it is a Super string or not. A String is Super if the number of uppercase letters are equal to the number of lower case letters. [Use Character & String methods only] Example : “COmmITmeNt” Number of Uppercase letters – 5 Number of Lowercase letters – 5 String is a Super String