Ever tried to rearrange the letters of a word to make a secret code? In Java, that kind of fun is called string handling.
A string is just a line of text – like a necklace of characters. Java lets you grab, cut, stitch, or compare those necklaces in many ways, all with simple commands.
What is a String in Java?
A String (capital S) is a built‑in object that stores a sequence of characters, such as letters, numbers, or symbols. Think of it as a train where each carriage holds one character. Once you create a string, Java keeps it safe and you can ask it questions or change it without breaking the train.
Creating Strings
You can make a string in two common ways:
String s = "Hello World";– the text is written directly between double quotes.String s = new String("Hello World");– you ask Java to build a new string object using thenewkeyword.
Both give you a string called s. The first style is shorter and is what most students use in exams.
Common String Methods
Methods are actions you can perform on a string, like tools in a toolbox. Below is a quick cheat‑sheet that shows the most useful ones for ICSE.
| Method | What it does | Example |
|---|---|---|
length() | Returns the number of characters in the string. | "Java".length() // 4 |
charAt(int index) | Gives the character at the specified position (starting at 0). | "Java".charAt(2) // 'v' |
substring(int start, int end) | Extracts part of the string from start (inclusive) to end (exclusive). | "Java".substring(1,3) // "av" |
concat(String str) | Joins another string to the end of the current one. | "Hello".concat(" World") // "Hello World" |
equals(Object obj) | Checks if two strings have exactly the same characters. | "abc".equals("ABC") // false |
compareTo(String another) | Compares two strings alphabetically; returns 0 if equal, negative if first is smaller, positive if larger. | "apple".compareTo("banana") // negative |
toUpperCase() | Changes every letter to its capital form. | "Java".toUpperCase() // "JAVA" |
toLowerCase() | Changes every letter to its small form. | "Java".toLowerCase() // "java" |
trim() | Removes blank spaces at the beginning and end. | " hello ".trim() // "hello" |
replace(char old, char new) | Swaps every occurrence of old with new. | "ball".replace('b','c') // "call" |
split(String regex) | Breaks the string into an array using a delimiter (like commas). | "a,b,c".split(",") // ["a","b","c"] |
Worked Example: Counting Vowels
Suppose the exam asks you to count how many vowels (a, e, i, o, u) are in a sentence. Here’s a clean way using the methods above:
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String input = sc.nextLine();
// Step 1: remove extra spaces
input = input.trim();
// Step 2: make everything lower case for easy checking
input = input.toLowerCase();
int count = 0;
for (int i = 0; i Notice how we used trim(), toLowerCase(), length(), and charAt() – all classic string tools.
Typical String Processing Flow
When a question asks you to clean and analyse text, you usually follow a tiny pipeline. The flowchart below shows the usual steps.
Tips for ICSE Exams
- Remember that
==checks if two string references point to the same object, not if the text is equal. Useequals()for content comparison. - String objects are immutable – once created, they cannot change. Methods like
concat()actually create a new string. - When you need many modifications, consider
StringBuilder(but for most ICSE questions, the basic methods are enough).
📝 Likely Exam Questions
- Write a program to reverse a string entered by the user.
Model answer: useStringBuilder sb = new StringBuilder(input); sb.reverse(); System.out.println(sb.toString()); - Explain the difference between
equals()and==for strings.
Model answer:equals()compares the actual characters;==compares memory addresses (whether both references point to the same object). - Given the string " Java Programming ", show the output of
trim(),toUpperCase(), andsubstring(5,13)in that order.
Model answer: Aftertrim()→ "Java Programming"; aftertoUpperCase()→ "JAVA PROGRAMMING"; aftersubstring(5,13)→ "PROGRAM". - Write a short code snippet to count how many times the word "java" appears in a paragraph (case‑insensitive).
Model answer: Convert paragraph to lower case, usesplit("java"), thencount = parts.length - 1.