In Java, the String class is used to create and manipulate strings. Strings in Java are immutable, meaning once a String object is created, its value cannot be changed. Here’s a guide to common string handling operations and methods in Java.

1. Creating Strings

Strings can be created in Java in several ways:

1.1 Using String Literals

  public class StringExample {
    public static void main(String[] args) {
        String str1 = "Hello, World!";
        System.out.println(str1);
    }
}
  

1.2 Using the new Keyword

  public class StringExample {
    public static void main(String[] args) {
        String str2 = new String("Hello, Java!");
        System.out.println(str2);
    }
}
  

2. Common String Methods

2.1 Length of a String

The length() method returns the number of characters in the string.

  public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, Java!";
        System.out.println("Length: " + str.length());
    }
}
  

2.2 Concatenation

Use the concat() method or the + operator to concatenate strings.

  public class StringExample {
    public static void main(String[] args) {
        String str1 = "Hello, ";
        String str2 = "Java!";
        String result = str1.concat(str2);
        System.out.println("Concatenated: " + result);
    }
}
  

2.3 Substring

The substring() method extracts a part of the string.

  public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, Java!";
        String subStr = str.substring(7, 11); // Extracts "Java"
        System.out.println("Substring: " + subStr);
    }
}
  

2.4 Replace

The replace() method replaces occurrences of a specified character or substring.

  public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, Java!";
        String newStr = str.replace("Java", "World");
        System.out.println("Replaced: " + newStr);
    }
}
  

2.5 Split

The split() method splits the string based on a delimiter.

  public class StringExample {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        String[] fruits = str.split(",");
        for (String fruit : fruits) {
            System.out.println("Fruit: " + fruit);
        }
    }
}
  

2.6 Trim

The trim() method removes leading and trailing whitespace.

  public class StringExample {
    public static void main(String[] args) {
        String str = "  Hello, Java!  ";
        String trimmedStr = str.trim();
        System.out.println("Trimmed: [" + trimmedStr + "]");
    }
}
  

2.7 Convert to Uppercase and Lowercase

The toUpperCase() and toLowerCase() methods convert the string to uppercase or lowercase.

  public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, Java!";
        System.out.println("Uppercase: " + str.toUpperCase());
        System.out.println("Lowercase: " + str.toLowerCase());
    }
}
  

2.8 Check Equality

Use equals() to compare strings for equality, and equalsIgnoreCase() for case-insensitive comparison.

  public class StringExample {
    public static void main(String[] args) {
        String str1 = "Hello, Java!";
        String str2 = "Hello, Java!";
        String str3 = "hello, java!";
        
        System.out.println("Equals: " + str1.equals(str2)); // true
        System.out.println("Equals Ignore Case: " + str1.equalsIgnoreCase(str3)); // true
    }
}
  

2.9 Index of a Substring

The indexOf() method returns the index of the first occurrence of a specified substring.

  public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, Java!";
        int index = str.indexOf("Java");
        System.out.println("Index of 'Java': " + index);
    }
}
  

3. StringBuilder and StringBuffer

For mutable strings, use StringBuilder and StringBuffer. StringBuilder is not synchronized and is faster, while StringBuffer is synchronized and thread-safe.

3.1 StringBuilder

  public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append(", Java!");
        System.out.println("StringBuilder: " + sb.toString());
    }
}
  

3.2 StringBuffer

  public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(", Java!");
        System.out.println("StringBuffer: " + sb.toString());
    }
}
  

4. String Formatting

Use String.format() or System.out.printf() to format strings.

Code Example:

  public class StringFormatExample {
    public static void main(String[] args) {
        int age = 30;
        String name = "John";
        String formatted = String.format("Name: %s, Age: %d", name, age);
        System.out.println(formatted);
    }
}