Demonstrating Few String Manipulation Functions in Java

Java’s String class provides a rich set of built-in methods for manipulating text. In this post, we demonstrate the most commonly used string functions — including case conversion, length, concatenation, trimming, character access, equality checks, and index searching — through a simple interactive Java program.

String Methods Covered

MethodDescription
toLowerCase()Converts all characters to lowercase
toUpperCase()Converts all characters to uppercase
length()Returns the number of characters in the string
concat()Appends one string to another
trim()Removes leading and trailing whitespace
charAt(index)Returns the character at the specified index (0-based)
equals()Compares two strings (case-sensitive)
equalsIgnoreCase()Compares two strings ignoring case
indexOf(char)Returns the first index of a character, or -1 if not found

Java Program: String Manipulation Demo

import java.io.*;

public class StringManipulation {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        // Read two input strings from the user
        System.out.print("Enter first string: ");
        String firstString = reader.readLine();

        System.out.print("Enter second string: ");
        String secondString = reader.readLine();

        // Display the original strings
        System.out.println("\n--- String Information ---");
        System.out.println("First string  : " + firstString);
        System.out.println("Second string : " + secondString);

        // Case conversion
        System.out.println("\n--- Case Conversion ---");
        System.out.println("First string in lowercase  : " + firstString.toLowerCase());
        System.out.println("Second string in uppercase : " + secondString.toUpperCase());

        // Length
        System.out.println("\n--- Length ---");
        System.out.println("Length of first string  : " + firstString.length());
        System.out.println("Length of second string : " + secondString.length());

        // Concatenation
        System.out.println("\n--- Concatenation ---");
        System.out.println("Concatenated string : " + firstString.concat(secondString));

        // Trim (removes leading/trailing whitespace)
        System.out.println("\n--- Trim ---");
        System.out.println("First string after trim : " + firstString.trim());

        // Character access (index 2 = third character, 0-based)
        System.out.println("\n--- Character Access (index 2) ---");
        System.out.println("3rd character of first string  : " + firstString.charAt(2));
        System.out.println("3rd character of second string : " + secondString.charAt(2));

        // Equality checks
        System.out.println("\n--- Equality ---");
        System.out.println("Strings are equal (case-sensitive)  : " + firstString.equals(secondString));
        System.out.println("Strings are equal (ignore case)     : " + firstString.equalsIgnoreCase(secondString));

        // Index of character
        System.out.println("\n--- Index Search ---");
        System.out.println("Index of 'a' in second string : " + secondString.indexOf('a'));
    }
}

How the Code Works

  1. Input — Two strings are read from the console using BufferedReader.
  2. toLowerCase() / toUpperCase() — Return new string objects with all letters converted; the original string is unchanged (strings are immutable in Java).
  3. length() — Returns the number of characters, including spaces and special characters.
  4. concat() — Returns a new string with the argument appended to the end. Equivalent to the + operator for strings.
  5. trim() — Strips whitespace from both ends. Useful when processing user input that may have accidental spaces.
  6. charAt(2) — Returns the character at index 2 (the third character, since indexing starts at 0).
  7. equals() — Performs a case-sensitive character-by-character comparison. Returns false if any character differs in case.
  8. equalsIgnoreCase() — Same comparison but treats uppercase and lowercase as equal.
  9. indexOf(‘a’) — Scans the string left-to-right for the first occurrence of the character 'a'. Returns its index or -1 if not found.

Sample Output

Enter first string: Ankur
Enter second string: Mhatre

--- String Information ---
First string  : Ankur
Second string : Mhatre

--- Case Conversion ---
First string in lowercase  : ankur
Second string in uppercase : MHATRE

--- Length ---
Length of first string  : 5
Length of second string : 6

--- Concatenation ---
Concatenated string : AnkurMhatre

--- Trim ---
First string after trim : Ankur

--- Character Access (index 2) ---
3rd character of first string  : k
3rd character of second string : a

--- Equality ---
Strings are equal (case-sensitive)  : false
Strings are equal (ignore case)     : false

--- Index Search ---
Index of 'a' in second string : 2

Output Explanation

  • Lowercase / Uppercase — “Ankur” → “ankur”; “Mhatre” → “MHATRE”.
  • charAt(2) — In “Ankur” the characters are A(0) n(1) k(2) u(3) r(4), so index 2 is ‘k’. In “Mhatre”, index 2 is ‘a’.
  • equals / equalsIgnoreCase — Both return false because “Ankur” and “Mhatre” are completely different strings.
  • indexOf(‘a’) — In “Mhatre”, ‘a’ first appears at index 2 (M=0, h=1, a=2).

See Also

Conclusion

Java’s String class is one of the most feature-rich classes in the standard library. The methods demonstrated here cover the most common text-processing tasks you’ll encounter in everyday programming. Remember that all string methods in Java return new strings rather than modifying the original, because Java strings are immutable by design.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.