String Handling ICSE Class 10 Computer Applications
Updated 14 October 2026 · Fact-checked
A String is an object of the String class holding a sequence of characters, indexed from 0. ICSE Class 10 covers 18 methods, including length(), charAt(), indexOf(), lastIndexOf(), substring(), concat(), equals(), compareTo(), replace(), trim(), startsWith(), endsWith() and valueOf(), plus programs that extract characters, sort strings alphabetically and search an array of strings.
What String Handling is about
String questions are guaranteed marks if you know each method's return type and index rules. Section A in 2024 asked "CONCENTRATION".indexOf('T'), "talent".compareTo("genius") and an error where compareTo() was stored in a boolean; 2025 asked "ANGER".compareTo("ANGEL"), a substring and concat output, a String method that returns only a positive integer, and a loop printing substrings. Section B had a gmail-id validation (2024) and a 'Super string' count (2025). The examiners' tips: write index numbers above the characters starting from 0, and find compareTo() differences without listing every ASCII code.
The syllabus lists the String class methods trim(), toLowerCase(), toUpperCase(), length(), charAt(), indexOf(char), lastIndexOf(char), concat(), equals(), equalsIgnoreCase(), compareTo(), compareToIgnoreCase(), replace(char, char), substring(begin), substring(begin, end), startsWith(), endsWith() and valueOf(), together with programs on extracting and modifying characters, alphabetical order of strings in an array (bubble and selection sort) and linear search of strings.
Weightage: CISCE publishes no chapter weightage. Section A (40 marks) is compulsory: 1-mark MCQs and 2-mark short answers including output and error questions; in Section B you answer any four of six 15-mark programming questions written in Java.Source: CISCE ICSE Computer Applications (661) specimen paper structure for 2027
Key concepts
- Strings, indexes and length
A String is an object of the String class (java.lang) that stores a sequence of characters: String s = "COMPUTER";. Each character has an index from 0 to length() − 1:
C O M P U T E R
0 1 2 3 4 5 6 7int length(): number of characters: "COMPUTER".length() = 8; "".length() = 0. It never returns a negative number.
char charAt(int n): the character at index n: s.charAt(3) = 'P'; s.charAt(s.length() - 1) = 'R'. An index outside the range is a runtime error.Strings are immutable: methods such as toUpperCase() or replace() return a new String and do not change the original unless you assign it back: s = s.toUpperCase();.
Worked example
(2 marks) Name (a) one String method that returns only a non-negative integer and (b) one that returns a character.
Try it first, then show the solutionSolution
- (a) length(): a count, never negative (indexOf and compareTo can be negative) - 1 mark.
- (b) charAt(int n) - 1 mark.
Answer: (a) length() (b) charAt(int)
- Searching inside a string: indexOf, lastIndexOf, startsWith, endsWith
int indexOf(char ch): index of the first occurrence of ch, or −1 if absent.
int lastIndexOf(char ch): index of the last occurrence, or −1.
"MISSISSIPPI" (M0 I1 S2 S3 I4 S5 S6 I7 P8 P9 I10):
indexOf('S') = 2; lastIndexOf('S') = 6; indexOf('P') = 8; indexOf('Z') = −1.boolean startsWith(String str) and boolean endsWith(String str) test the beginning and end:
"report2027.pdf".endsWith(".pdf") → true; "report2027.pdf".startsWith("Report") → false (case matters).Worked example
(2 marks) String w = "BANANA"; Give w.indexOf('N') + w.lastIndexOf('A') and w.indexOf('K').
Try it first, then show the solutionSolution
- B0 A1 N2 A3 N4 A5: indexOf('N') = 2, lastIndexOf('A') = 5; sum 7 - 1 mark.
- 'K' is absent: −1 - 1 mark.
Answer: 7 and -1
- Extracting and joining: substring, concat, trim, case, replace, valueOf
String substring(int begin): from index begin to the end. "COMPUTER".substring(3) = "PUTER".
String substring(int begin, int end): from begin up to end − 1 (end excluded); length = end − begin. "COMPUTER".substring(1, 4) = "OMP".
String concat(String str): joins: "Blue".concat("J") = "BlueJ" (same as + with strings).
String trim(): removes leading and trailing spaces (not inner ones): " Java ".trim() = "Java".
String toUpperCase() / toLowerCase(): whole string in one case.
String replace(char old, char new): replaces every occurrence: "banana".replace('a', 'o') = "bonono".
String valueOf(any type): converts a value to a String (static): String.valueOf(25) + 5 = "255"; String.valueOf('x') = "x".Combined example: "Holiday".substring(4).concat("mobile".substring(4)) = "day" + "le" = "dayle".
Worked example
(2 marks) Give the output: System.out.println("Holiday".substring(4).concat("mobile".substring(4)));
Try it first, then show the solutionSolution
- "Holiday": H0 o1 l2 i3 d4 a5 y6 → substring(4) = "day" - 1 mark.
- "mobile": m0 o1 b2 i3 l4 e5 → substring(4) = "le"; joined "dayle" - 1 mark.
Answer: dayle
- Comparing strings: equals, equalsIgnoreCase, compareTo, compareToIgnoreCase
boolean equals(String str): true if both have exactly the same characters (case sensitive). "Java".equals("java") → false.
boolean equalsIgnoreCase(String str): ignores case. "Java".equalsIgnoreCase("JAVA") → true.
Use equals(), not ==, to compare String contents (== checks whether they are the same object).int compareTo(String str): compares in dictionary (Unicode) order:
- 0 if equal;
- otherwise, at the first position where they differ, (code of this char) − (code of the other char);
- if one string is a prefix of the other, (length of this) − (length of the other).
A negative result means the calling string comes first alphabetically.
"apple".compareTo("apricot"): first difference p (112) vs r (114) → −2.
"Zebra".compareTo("apple"): 'Z' (90) − 'a' (97) = −7 (capitals come before small letters).
"CAT".compareTo("CATALOG"): prefix → 3 − 7 = −4.
int compareToIgnoreCase(String str): the same, ignoring case: "Zebra".compareToIgnoreCase("apple") = 'z' − 'a' = 25.Shortcut: count letters apart instead of using codes: 't' − 'g' = 13 because t is 13 letters after g.
Worked example
(2 marks) String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); gives 'incompatible types: int cannot be converted to boolean'. Correct the statement and give the output of printing the corrected value.
Try it first, then show the solutionSolution
- compareTo returns int, so store it in an int: int x = a.compareTo(b); - 1 mark.
- KING is a prefix of KINGDOM: 4 − 7 = −3 - 1 mark.
Answer: int x = a.compareTo(b); prints -3 (or use boolean x = a.equals(b); which prints false).
- String programs: extracting characters, sorting and searching
Character-by-character processing:
for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
... test or change ch ...
}
Building a new string: res = res + ch; (start with res = "").Alphabetical order of a String array uses compareTo() inside bubble or selection sort:
if (names[j].compareTo(names[j + 1]) > 0) swap them;Linear search of a String array uses equals() (or equalsIgnoreCase()):
for (int i = 0; i < n; i++)
if (names[i].equalsIgnoreCase(key)) { pos = i; break; }Other common tasks: counting vowels, words (count spaces + 1 after trim), reversing a word, checking a palindrome (compare with its reverse using equalsIgnoreCase), capitalising the first letter of each word.
Worked example
(2 marks) Give the output and the number of times the loop runs:
String s = "BLUEJ";
for (int i = 1; i < s.length(); i += 2)
System.out.println(s.substring(0, i));Try it first, then show the solutionSolution
- i = 1: substring(0, 1) = "B"; i = 3: substring(0, 3) = "BLU" - 1 mark.
- i = 5 is not < 5: loop runs 2 times - 1 mark.
Answer: B
BLU
(the loop runs 2 times)
String Handling in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
endsWith for validation
Suppose a school portal accepts only email addresses ending with "@school.edu.in" when students register.
Which String method checks this, and what would it return for "riya08@school.edu.in"?
Try it first, then show the solutionSolution
- Checking the end of a string: endsWith().
- "riya08@school.edu.in".endsWith("@school.edu.in") is true.
- A stricter check could also confirm there is text before '@' using indexOf('@') > 0.
Answer: endsWith("@school.edu.in"), which returns true for that address.
Memory hook: Check the tail with endsWith, the head with startsWith.
compareTo for sorting
Imagine a phone's contact list must show names in alphabetical order, so 'Arjun' appears before 'Kabir'.
How does Java decide the order of two names?
Try it first, then show the solutionSolution
- "Arjun".compareTo("Kabir") = 'A' − 'K' = 65 − 75 = −10.
- Negative means Arjun comes first.
- A sort swaps two names when compareTo() is positive.
Answer: compareTo() gives a negative number when the first name comes earlier in dictionary order; sorting uses this to arrange names.
Memory hook: Negative = before; positive = after; zero = same.
trim and equalsIgnoreCase
Suppose a quiz app accepts the answer 'Delhi' but a student types ' delhi ' with spaces and a small d.
How should the app compare the answer fairly?
Try it first, then show the solutionSolution
- trim() removes the extra spaces at the ends.
- equalsIgnoreCase() ignores the difference in case.
- ans.trim().equalsIgnoreCase("Delhi") is true.
Answer: Use ans.trim().equalsIgnoreCase("Delhi"), which returns true.
Memory hook: Trim the edges, ignore the case.
Topics in this chapter
String Handling: syntax and key facts
- Return types
- int: length, indexOf, lastIndexOf, compareTo, compareToIgnoreCase; char: charAt; boolean: equals, equalsIgnoreCase, startsWith, endsWith; String: trim, toLowerCase, toUpperCase, concat, replace, substring, valueOf
- Only length() is never negative.
- substring
- substring(b) = from b to end; substring(b, e) = b to e − 1; length e − b
- End index excluded.
- compareTo
- first mismatch: code(this) − code(other); prefix: length(this) − length(other); equal: 0
- 'A' 65, 'a' 97; capitals sort first.
- Sorting strings
- bubble: if (a[j].compareTo(a[j+1]) > 0) swap
- Search with equals()/equalsIgnoreCase().
How the board asks String Handling
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| Output MCQ (Section A) | indexOf of a character (2024); compareTo of two words (2024, 2025). | Write indexes from 0 above the letters; for compareTo find the first mismatch and count letters apart. |
| Output / error (2 marks) | substring with concat (2025); compareTo stored in a boolean (2024); methods returning a positive int or a char (2025); substring in a loop (2025). | Know each method's return type and the end-excluded rule. |
| Program (15 marks) | Validate a gmail id (2024); Super string with equal upper- and lower-case counts (2025); sort or search names. | Loop over charAt(i); use the listed String and Character methods; print both outcomes. |
Tricks to remember String Handling
Short memory aids. Say each one aloud once, then use it on a question the same day.
Zero-start, end-excluded
Indexes start at 0; substring's end index is not included.
Use it when: charAt and substring outputs.
First mismatch decides
compareTo looks only at the first different letter (or the lengths if one is a prefix).
Use it when: compareTo outputs.
Strings never change
String methods return a new String; assign it if you want to keep it.
Use it when: toUpperCase, replace, trim.
String Handling: board-level solved questions
Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: indexOf · 1 mark · MCQBoard level
"ALGORITHM".indexOf('R') returns:
(a) 4
(b) 3
(c) 5
(d) -1
Try it first, then show the solutionSolution
- A0 L1 G2 O3 R4: first R at index 4.
Answer: (a) 4
Example 2: compareTo · 1 mark · MCQHarder board level
"tiger".compareTo("lion") returns:
(a) 8
(b) -8
(c) 1
(d) 0
Try it first, then show the solutionSolution
- First letters differ: 't' − 'l'; t is 8 letters after l.
Answer: (a) 8
Example 3: Assertion and reason: equals vs == · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): equals() should be used to compare the contents of two Strings.
Reason (R): equals() is case insensitive.
(a) Both A and R are true and R is the correct explanation of A
(b) Both A and R are true but R is not the correct explanation of A
(c) A is true but R is false
(d) A is false but R is true
Try it first, then show the solutionSolution
- A is true. R is false: equals() is case sensitive; equalsIgnoreCase() ignores case.
Answer: (c) A is true but R is false
Example 4: Chain of methods · 2 marks · Short answerHarder board level
Give the output:
String s = " Exam Ready ";
String t = s.trim();
System.out.println(t.length() + " " + t.toUpperCase().indexOf('R') + " " + t.replace('a', '@'));
Try it first, then show the solutionSolution
- t = "Exam Ready" (10 characters). Upper case "EXAM READY": E0 X1 A2 M3 (space)4 R5 → 5.
- replace('a', '@') on "Exam Ready": E x @ m R e @ d y → "Ex@m Re@dy".
Answer: 10 5 Ex@m Re@dy
Example 5: compareTo with a prefix and case · 2 marks · Short answerHOTS / competency
Give the values: (i) "CAT".compareTo("CATALOG") (ii) "Zebra".compareTo("apple") (iii) "Zebra".compareToIgnoreCase("apple")
Try it first, then show the solutionSolution
- (i) CAT is a prefix: 3 − 7 = −4.
- (ii) 'Z' 90 − 'a' 97 = −7. (iii) 'z' 122 − 'a' 97 = 25.
Answer: (i) -4 (ii) -7 (iii) 25
Example 6: Capitalise each word (Section B) · 15 marks · Long answerHarder board level
Write a program to accept a sentence in lower case and print it with the first letter of every word in capitals. Example: "the quick brown fox" → "The Quick Brown Fox".
Try it first, then show the solutionSolution
- import java.util.Scanner;
class TitleCase
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Sentence: ");
String s = sc.nextLine().trim();
String res = ""; - for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if (i == 0 || s.charAt(i - 1) == ' ')
res = res + Character.toUpperCase(ch);
else
res = res + ch;
} - System.out.println(res);
}
} - A letter starts a word if it is the first character or follows a space; i == 0 is tested first, so s.charAt(i - 1) is never called with −1.
- Trace: t (i = 0) → T; q follows a space → Q; b → B; f → F; others unchanged.
Answer: Sample run:
Sentence: the quick brown fox
The Quick Brown Fox
Example 7: Sort names alphabetically · 15 marks · Long answerHOTS / competency
Write a program to store the names Neha, Arjun, Zoya, Kabir, Meera in an array, sort them in alphabetical order using bubble sort, and then search for a name entered by the user (ignoring case) using linear search.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class Names
{
public static void main(String args[])
{
String a[] = {"Neha", "Arjun", "Zoya", "Kabir", "Meera"};
int n = a.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (a[j].compareTo(a[j + 1]) > 0)
{
String t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
} - for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println(); - Scanner sc = new Scanner(System.in);
System.out.print("Search: ");
String key = sc.next();
int pos = -1;
for (int i = 0; i < n; i++)
if (a[i].equalsIgnoreCase(key))
{
pos = i;
break;
}
if (pos >= 0)
System.out.println("Found at position " + (pos + 1));
else
System.out.println("Not found");
}
} - Pass 1: Arjun Neha Zoya Kabir Meera → Arjun Neha Kabir Zoya Meera → Arjun Neha Kabir Meera Zoya. Pass 2: Arjun Kabir Neha Meera Zoya → Arjun Kabir Meera Neha Zoya. Passes 3 and 4: no swaps.
- Search 'meera': equalsIgnoreCase matches index 2 → position 3.
Answer: Sample run:
Arjun Kabir Meera Neha Zoya
Search: meera
Found at position 3
Case-study practice (Section B style)
Read the situation, then answer every part. In the board paper this is a 10-mark question split into 2 + 3 + 5 marks.
Checking roll codes
Imagine a school's roll codes look like "X10A-27": class letter, class number, section, a hyphen and a two-digit roll number. A teacher's program has String code = "X10A-27";.
(i) · 2 marks
Give the values of code.length() and code.indexOf('-').
Answer
Answer: length() = 7; X0 1(1) 0(2) A3 -4 2(5) 7(6), so indexOf('-') = 4.
1 mark each.
(ii) · 3 marks
Write expressions to extract the section letter and the roll number as a String and as an int.
Answer
Answer: Section: code.charAt(3) → 'A'. Roll as String: code.substring(code.indexOf('-') + 1) → "27". As int: Integer.parseInt(code.substring(5)) → 27.
1 mark each.
(iii) · 5 marks
Write a condition that checks a code is valid: it starts with "X", has a '-' at index 4, and its last two characters are digits. Evaluate it for "X10A-27" and "X10A27".
Answer
Answer: code.startsWith("X") && code.length() == 7 && code.indexOf('-') == 4 && Character.isDigit(code.charAt(5)) && Character.isDigit(code.charAt(6)). For "X10A-27": true. For "X10A27": length is 6, so false (the length test also prevents charAt(6) from failing).
Condition 3 marks; two evaluations 1 mark each.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
Give the output of substring and concat. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| Each substring correct (0-based, end excluded). | 1 |
| Joined result correct. | 1 |
String program: sort names and search (bubble sort + linear search). (15 marks) (15 marks)
| Step | Marks |
|---|---|
| Array initialised as given. | 2 |
| Bubble sort loops with correct bounds. | 3 |
| compareTo(...) > 0 condition and String swap. | 3 |
| Display of sorted names. | 2 |
| Key accepted; linear search with equalsIgnoreCase. | 3 |
| One correct message. | 2 |
Where students lose marks in String Handling
The slips examiners see most often, and what to write instead.
Mistake: Counting index from 1
The 2024 examiners reported candidates chose 7 for "CONCENTRATION".indexOf('T') by counting from 1.
Do this: Write 0, 1, 2 ... above the letters before answering.
Usually costs: 1 mark.
Mistake: Not knowing how compareTo() works
In 2024 candidates were confused by the sign and wrote out all ASCII codes.
Do this: Find the first mismatch; subtract (this − other); count letters apart.
Usually costs: 1 mark.
Mistake: Naming compareTo or indexOf as 'positive only'
In 2025 candidates gave these for a method returning only a positive integer, and Character methods for a String method returning a char.
Do this: length() is never negative; charAt() returns a char.
Usually costs: 1 mark.
Mistake: Copying the code instead of correcting it
In 2024 candidates copied the compareTo-in-boolean question without studying it.
Do this: Read the error message: int cannot be converted to boolean → store compareTo() in an int.
Usually costs: 2 marks.
Mistake: Ad hoc logic for validation
In the 2024 gmail program candidates used varied, unsuitable logic.
Do this: Use indexOf, substring, startsWith and endsWith to test each required part.
Usually costs: Up to 10 marks.
Practice questions on String Handling
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
"PROGRAM".lastIndexOf('R') returns:
(a) 1
(b) 4
(c) 5
(d) -1Hint
P0 R1 O2 G3 R4.
Answer
(b) 4
Q2 · 1 mark · MCQHarder board level
"apple".compareTo("apricot") returns:
(a) 2
(b) -2
(c) 0
(d) -3Hint
Third letters p and r.
Answer
(b) -2
Q3 · 1 mark · MCQBoard level
Which method returns a boolean?
(a) indexOf()
(b) concat()
(c) endsWith()
(d) charAt()Hint
Yes or no.
Answer
(c) endsWith()
Q4 · 2 marks · Very short answerHarder board level
Give the output: System.out.println("COMPUTER".substring(1, 4) + String.valueOf(25) + 5);
Hint
End excluded; string joins.
Answer
OMP255
Q5 · 2 marks · Very short answerBoard level
Distinguish between equals() and compareTo().
Hint
Return types.
Answer
equals() returns a boolean telling whether two strings are exactly the same; compareTo() returns an int: 0 if equal, negative if the calling string comes first in dictionary order, positive if it comes after.
Q6 · 2 marks · Short answerHOTS / competency
Give the output:
String s = "level";
String r = "";
for (int i = s.length() - 1; i >= 0; i--)
r = r + s.charAt(i);
System.out.println(r + " " + r.equals(s));Hint
Reverse.
Answer
level true
Q7 · 15 marks · Long answerHarder board level
Write a program to accept a word and check whether it is a palindrome, ignoring case (e.g. "Madam").
Hint
Build the reverse.
Answer
import java.util.Scanner;
class Palin
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Word: ");
String w = sc.next();
String rev = "";
for (int i = w.length() - 1; i >= 0; i--)
rev = rev + w.charAt(i);
if (rev.equalsIgnoreCase(w))
System.out.println("Palindrome");
else
System.out.println("Not a palindrome");
}
}
Trace "Madam": rev = "madaM"; equalsIgnoreCase → Palindrome. "Java": rev "avaJ" → Not a palindrome.Q8 · 15 marks · Long answerHOTS / competency
Write a program to accept a sentence and count the number of words and the number of times the letter 'e' (either case) occurs.
Hint
Words = spaces + 1 after trim (single spaces).
Answer
import java.util.Scanner;
class Count
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Sentence: ");
String s = sc.nextLine().trim();
int words = 1, e = 0;
for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if (ch == ' ') words++;
if (ch == 'e' || ch == 'E') e++;
}
System.out.println("Words = " + words + ", e = " + e);
}
}
Trace "Every exam needs preparation": 3 spaces → 4 words; e's: E, e (Every 2: E, e), exam 1, needs 2, preparation 1 → 6. Output: Words = 4, e = 6. (Assumes single spaces between words.)Q9 · 2 marks · Short answerHOTS / competency
Why does "Java" == new String("Java") give false while "Java".equals(new String("Java")) gives true?
Hint
Objects vs contents.
Answer
== checks whether both refer to the same object; new String creates a different object, so it is false. equals() compares the characters, which are the same, so it is true.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1."HELLO".charAt(1)?Show answer
'E'
2."HELLO".substring(1, 3)?Show answer
"EL"
3.indexOf when the character is absent?Show answer
-1
4.Which compares ignoring case and returns boolean?Show answer
equalsIgnoreCase()
5.Does toUpperCase() change the original String?Show answer
No; it returns a new String.
Best order to study it
- Indexes, length() and charAt().
- indexOf, lastIndexOf, startsWith, endsWith.
- substring, concat, trim, case methods, replace, valueOf.
- equals, equalsIgnoreCase, compareTo, compareToIgnoreCase.
- Programs: character processing, palindrome, word count.
- Sorting a String array and linear search.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Index 0 to length() − 1; charAt(i).
- indexOf/lastIndexOf: −1 if absent.
- substring(b, e): e excluded.
- compareTo: first mismatch difference, or length difference; 0 if equal.
- equals: case sensitive; use it, not ==.
- Strings are immutable: assign the result.
String Handling: frequently asked questions
Are StringBuffer methods in the syllabus?
No. Only the 18 String class methods listed are required.
Can indexOf take a String argument?
Java allows it, but the syllabus lists indexOf(char) and lastIndexOf(char); questions use a character.
How do I convert a number to a String?
Use String.valueOf(n), or join it with an empty string: "" + n.
Based on: CISCE ICSE 2027 Computer Applications syllabus; CISCE Analysis of Pupil Performance ICSE 2025: Computer Applications (with the 2025 board paper)