Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Master Java String Manipulation: Essential Functions, Methods, and Practical Examples

What Are Strings?

In Java, a String is an immutable sequence of characters, essentially a specialized array that represents text data. For example, the word ROSE can be visualized as:

Master Java String Manipulation: Essential Functions, Methods, and Practical Examples

In this guide you’ll learn:

Why Use Strings?

Text processing is a core part of software development—menus, dialogs, logs, and APIs all rely on it. The Java String class provides a reliable, type‑safe way to store and manipulate textual data, enabling developers to build user‑friendly interfaces and perform complex string operations with minimal overhead.

String Syntax Examples

Although a string can be created from a character array, the most common declaration is a string literal:

// Using a character array
char[] arrSample = {'R', 'O', 'S', 'E'};
String strSample1 = new String(arrSample);

// Direct string literal
String strSample2 = "ROSE";

The literal form automatically places the string in the String Constant Pool, saving memory and improving performance.

String Concatenation

Concatenation joins two or more strings into one. Java offers two convenient methods:

Example:

public class SampleString {
    public static void main(String[] args) {
        String str1 = "Rock";
        String str2 = "Star";
        // Method 1: concat
        String str3 = str1.concat(str2);
        System.out.println(str3);
        // Method 2: + operator
        String str4 = str1 + str2;
        System.out.println(str4);
    }
}

Key Java String Methods

Below are essential methods every Java developer should master, each illustrated with clear examples.

length()

String strSample = "RockStar";
System.out.println("Length of String: " + strSample.length());

Output: Length of String: 8

indexOf()

String strSample = "RockStar";
System.out.println("Character at position 5: " + strSample.charAt(5));
System.out.println("Index of 'S': " + strSample.indexOf('S'));

Output:
Character at position 5: t
Index of 'S': 4

charAt()

String strSample = "RockStar";
System.out.println("Character at position 5: " + strSample.charAt(5));

Output: Character at position 5: t

compareTo() & compareToIgnoreCase()

String strSample = "RockStar";
System.out.println("Compare To 'rockstar': " + strSample.compareTo("rockstar"));
System.out.println("Compare To 'ROCKSTAR' (ignore case): " + strSample.compareToIgnoreCase("ROCKSTAR"));

Output:
Compare To 'rockstar': -32
Compare To 'ROCKSTAR' (ignore case): 0

contains()

String strSample = "RockStar";
System.out.println("Contains 'tar': " + strSample.contains("tar"));

Output: Contains 'tar': true

endsWith()

String strSample = "RockStar";
System.out.println("Ends with 'r': " + strSample.endsWith("r"));

Output: Ends with 'r': true

replace() / replaceAll() / replaceFirst()

String strSample = "RockStar";
System.out.println("Replace 'Rock' with 'Duke': " + strSample.replace("Rock", "Duke"));

Output: Replace 'Rock' with 'Duke': DukeStar

toLowerCase() & toUpperCase()

String strSample = "RockStar";
System.out.println("Lower case: " + strSample.toLowerCase());
System.out.println("Upper case: " + strSample.toUpperCase());

Output:
Lower case: rockstar
Upper case: ROCKSTAR

Important Points to Note

That’s all there is to strings—keep these fundamentals in mind, and you’ll write cleaner, more efficient Java code.

Java

  1. Java Abstract Classes and Methods: A Comprehensive Guide
  2. Java Variables and Data Types – A Comprehensive Guide with Examples
  3. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  4. Java String length() Method: How to Get a String’s Size (Example)
  5. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  6. Java String.contains() Method: How to Check for Substrings – Practical Examples
  7. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  8. Java String toLowerCase() & toUpperCase(): Convert Text Case with Locale Awareness
  9. Mastering Java's split() Method: A Practical Guide with Code Examples
  10. Convert JSON to XML in Java with Gson & JAXB – Step‑by‑Step Guide