Skip to content
Computer ScienceISC Class 11 · Arrays and Strings

Java String Methods and String Programs (ISC Class 11 Computer Science)

Updated 17 October 2026 · Fact-checked

A String in Java is an object holding a sequence of characters; it is immutable, and its methods return new values. Key methods: length(), charAt(i), indexOf(ch), lastIndexOf(ch), substring(a, b) (from a to b - 1), toUpperCase(), trim(), replace(), equals(), equalsIgnoreCase() and compareTo(). Typical programs count vowels, reverse a string and test for a palindrome by looping over characters.

UML class diagram: Object at top with toString(); Integer, String (with substring()) and LinkedList inherit from it, each with their own met

String is a class that inherits from Object and adds its own methods such as substring(), so a String is an object with methods.

Picture: Pronesto · CC BY-SA 3.0 · Wikimedia Commons

Understand The String Class: Methods and String Programs in Java

String basics. Strings are objects of the class String; indexing starts at 0. A String is *immutable*: methods such as toUpperCase() return a new string and leave the original unchanged. Use equals() to compare contents; == compares references.

Common methods. For String s = "Computer Science":

MethodResult
s.length()16
s.charAt(3)'p'
s.indexOf('e')6 (first occurrence)
s.lastIndexOf('e')15
s.substring(3, 8)"puter" (index 3 to 7)
s.substring(9)"Science"
s.toUpperCase()"COMPUTER SCIENCE"
s.replace('e', 'a')"Computar Scianca"
s.trim()removes leading and trailing spaces

Comparison. s1.equals(s2) tests equal content; equalsIgnoreCase() ignores case; s1.compareTo(s2) returns 0 if equal, a negative value if s1 comes first, positive otherwise. It returns the difference of the first differing characters, or the difference of the lengths if one is a prefix of the other.

Concatenation. + joins strings; evaluated left to right: "abc" + 1 + 2 is "abc12" but 1 + 2 + "abc" is "3abc".

Standard programs.

String r = "";
for (int i = s.length() - 1; i >= 0; i--)
    r = r + s.charAt(i);          // reverse
int v = 0;
for (int i = 0; i < s.length(); i++) {
    char c = Character.toLowerCase(s.charAt(i));
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') v++;   // vowels
}

A palindrome equals its reverse. Word count: count spaces and add 1 (for single spaces).

The String Class: Methods and String Programs in Java: key facts

substring
s.substring(a, b) = characters a to b - 1 | length = b - a
End index excluded.
compareTo
s1.compareTo(s2) = ASCII difference of first mismatch | Else length difference
0 means equal.
Last index
Last index = s.length() - 1 | Loop i from 0 to length - 1
Zero-based.

How to answer The String Class: Methods and String Programs in Java questions

Index the characters before applying a method.

  1. 1Write the string with indices under each character.
  2. 2Apply the method exactly: watch the end index of substring.
  3. 3For concatenation, evaluate left to right.
  4. 4For programs, trace the loop with the first two iterations.

The String Class: Methods and String Programs in Java in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Immutability

Suppose a program stores a user name "riya" and calls name.toUpperCase() without saving the result.

What does the variable name hold afterwards?

Try it first, then show the solutionSolution
  1. String methods return a new string.
  2. The result was not assigned, so name is still "riya".

Answer: "riya": the original is unchanged.

Memory hook: Assign the result or lose it.

Tricks to remember The String Class: Methods and String Programs in Java

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • End index is excluded

    substring(3, 8) stops before 8.

    Use it when: substring.

  • Equals not ==

    Use equals() for content.

    Use it when: Comparison.

  • Left to right

    1 + 2 + "a" is "3a"; "a" + 1 + 2 is "a12".

    Use it when: Concatenation.

The String Class: Methods and String Programs in Java: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: charAt · 1 mark · MCQBoard level

For String s = "Computer Science"; the value of s.charAt(3) is:
(a) 'm' (b) 'p' (c) 'u' (d) 't'

Try it first, then show the solutionSolution
  1. C0 o1 m2 p3.

Answer: (b) 'p'

Example 2: Output of methods · 3 marks · Short answerBoard level

Give the output: String s = "Computer Science"; System.out.println(s.length() + " " + s.indexOf('e') + " " + s.lastIndexOf('e') + " " + s.substring(3, 8) + " " + s.substring(9)); (3 marks)

Try it first, then show the solutionSolution
  1. Length = 16 (8 + 1 + 7).
  2. indexOf('e') = 6; lastIndexOf('e') = 15.
  3. substring(3, 8) = "puter"; substring(9) = "Science".

Answer: 16 6 15 puter Science

Example 3: compareTo · 5 marks · Short answerHarder board level

Find the values of (i) "apple".compareTo("banana") (ii) "Zebra".compareTo("apple") (iii) "Computer".compareTo("Compute") (iv) "Java".equals("java") and (v) "Java".equalsIgnoreCase("java"). (5 marks)

Try it first, then show the solutionSolution
  1. (i) 'a' - 'b' = 97 - 98 = -1.
  2. (ii) 'Z' - 'a' = 90 - 97 = -7.
  3. (iii) One is a prefix of the other: length difference 8 - 7 = 1.
  4. (iv) false (case differs). (v) true.

Answer: (i) -1; (ii) -7; (iii) 1; (iv) false; (v) true.

Example 4: Program: vowels and reverse · 6 marks · Long answerHarder board level

Write a program fragment to count the vowels and print the reverse of String s = "Education", and give the output. (6 marks)

Try it first, then show the solutionSolution
  1. Loop over each character; check against a, e, i, o, u in lower case.
  2. Vowels in "Education": E, u, a, i, o = 5.
  3. Reverse by appending characters from the last index: "noitacudE".

Answer:

String s = "Education";
int v = 0;
String r = "";
for (int i = 0; i < s.length(); i++) {
    char c = Character.toLowerCase(s.charAt(i));
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') v++;
}
for (int i = s.length() - 1; i >= 0; i--) r = r + s.charAt(i);
System.out.println(v + " " + r);
Output: 5 noitacudE

Example 5: Concatenation traps · 5 marks · Short answerHOTS / competency

Give the output: (a) System.out.println("abc" + 1 + 2); (b) System.out.println(1 + 2 + "abc"); (c) System.out.println("Sum=" + (1 + 2)); (d) String t = "java"; t.toUpperCase(); System.out.println(t); and say what is learnt from (d). (5 marks)

Try it first, then show the solutionSolution
  1. (a) "abc" + 1 gives "abc1", then + 2 gives "abc12".
  2. (b) 1 + 2 = 3 first, then "3abc".
  3. (c) The bracket is evaluated first: Sum=3.
  4. (d) toUpperCase() returns a new string that was not stored: prints java.
  5. Strings are immutable.

Answer: (a) abc12; (b) 3abc; (c) Sum=3; (d) java: strings are immutable, so the result must be assigned.

Where students lose marks in The String Class: Methods and String Programs in Java

The slips examiners see most often, and what to write instead.

  • Mistake: Counting the end index in substring

    Inclusive habit.

    Wrong working

    substring(3, 8) = "puters"

    Right working

    "puter"

    Do this: End index is excluded.

    Usually costs: 1-2 marks.

  • Mistake: Using == to compare strings

    Works for numbers.

    Wrong working

    if (s1 == s2)

    Right working

    if (s1.equals(s2))

    Do this: Compare content.

    Usually costs: 1-2 marks.

  • Mistake: Forgetting to store toUpperCase()

    Thinking it changes the string.

    Wrong working

    s.toUpperCase();

    Right working

    s = s.toUpperCase();

    Do this: Assign the result.

    Usually costs: 1 mark.

Exam tips

  • Write the index under each character in tracing questions.
  • Give output in the exact printed form (spacing).
  • State that String is immutable when asked about toUpperCase-style calls.

Practice questions on The String Class: Methods and String Programs in Java

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    The value of "Programming".substring(3, 7) is:
    (a) gram (b) gramm (c) ogra (d) ogram

    Hint

    P0 r1 o2 g3.

    Answer

    (a) gram

  2. Q2 · 3 marks · Short answerBoard level

    Write the output of: String a = "Hello World"; System.out.println(a.indexOf('o') + " " + a.lastIndexOf('o') + " " + a.toUpperCase() + " " + a.replace('l', 'L')); (3 marks)

    Hint

    H0 e1 l2 l3 o4.

    Answer

    4 7 HELLO WORLD HeLLo WorLd

  3. Q3 · 5 marks · Long answerHarder board level

    Write a program fragment to test whether String w = "level" is a palindrome and state its output. Also give the output for w = "Level". (5 marks)

    Hint

    Reverse and compare with equals.

    Answer

    Build r by reversing w and test w.equals(r): for "level" r = "level" and the output is "Palindrome". For "Level" r = "leveL"; equals is false (case differs), so the output is "Not a palindrome" unless equalsIgnoreCase is used.

  4. Q4 · 4 marks · Short answerHarder board level

    A sentence String t = "to be or not to be" has single spaces. Write the logic to count the words and the number of characters that are not spaces; give both answers. (4 marks)

    Hint

    Count spaces first.

    Answer

    Words = number of spaces + 1 = 5 + 1 = 6. Length = 18; spaces = 5; non-space characters = 13.

  5. Q5 · 3 marks · Short answerHOTS / competency

    A student writes String s = "ISC"; s.concat("Exam"); System.out.println(s + s.length()); and expects ISCExam6. Give the actual output and explain. (3 marks)

    Hint

    Immutable.

    Answer

    Output: ISC3. concat() returns a new string "ISCExam" which is not assigned, so s is unchanged and its length is 3.

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. 1.String index starts at?Show answer

    0.

  2. 2.substring(a, b) end index?Show answer

    b - 1.

  3. 3.Compare content with?Show answer

    equals().

  4. 4.Is String mutable?Show answer

    No, immutable.

  5. 5."a" + 1 + 2 gives?Show answer

    a12.

The String Class: Methods and String Programs in Java: frequently asked questions

What does compareTo return for equal strings?

0.

How to convert case?

toUpperCase() or toLowerCase().

Is length() a method or field?

A method for String; a field for arrays.

What does trim() do?

Removes leading and trailing spaces.