Search⌘ K
AI Features

Strings Operations

Explore essential string operations in Java that enable you to change case, search, extract, and clean text data. Understand how to handle user input, perform substring operations, and inspect string content while respecting the immutability of strings.

Once we know how to create and compare strings, we need to know how to manipulate them. Real-world data is rarely perfectly formatted. Users add accidental spaces to form inputs, system logs embed values within long text lines, and search features require case-insensitive matching. Java provides a rich API for extracting, cleaning, and inspecting text. We will explore the most essential methods for transforming string data into exactly what our applications need.

Changing case

Text comparisons are often case-sensitive in Java. "admin" and "Admin" are completely different strings. To normalize text for comparisons or display, we use .toLowerCase() and .toUpperCase().

Remember that strings are immutable. These methods do not alter the existing string; they return a brand new string with the casing changed.

Java 25
class ChangeCase {
public static void main(String[] args) {
String role = "SuperAdmin";
String lower = role.toLowerCase();
String upper = role.toUpperCase();
System.out.println("Original: " + role);
System.out.println("Lowercase: " + lower);
System.out.println("Uppercase: " + upper);
}
}
  • Line 3: We define a mixed-case string role.

  • Line 5: .toLowerCase() ...