Java Strings

Strings in Java are objects that represent sequences of characters. They are widely used in Java programming and are immutable, meaning that once a string is created, it cannot be changed.

Key Topics

1. Creating Strings

Strings can be created using string literals or by using the new keyword.

Example

public class CreatingStrings {
    public static void main(String[] args) {
        // Using string literal
        String str1 = "Hello, World!";

        // Using new keyword
        String str2 = new String("Hello, World!");

        System.out.println("str1: " + str1);
        System.out.println("str2: " + str2);
    }
}

Output:

str1: Hello, World!
str2: Hello, World!

2. String Methods

Java provides a variety of methods to perform operations on strings. Some commonly used methods are:

  • length(): Returns the length of the string.
  • charAt(int index): Returns the character at the specified index.
  • substring(int beginIndex, int endIndex): Returns a substring from the specified begin index to the end index.
  • toLowerCase(): Converts all characters in the string to lowercase.
  • toUpperCase(): Converts all characters in the string to uppercase.
  • trim(): Removes leading and trailing whitespace.
  • replace(char oldChar, char newChar): Replaces all occurrences of the specified old character with the new character.

Example

public class StringMethods {
    public static void main(String[] args) {
        String str = "  Hello, World!  ";
        System.out.println("Length: " + str.length());
        System.out.println("Character at index 1: " + str.charAt(1));
        System.out.println("Substring (7, 12): " + str.substring(7, 12));
        System.out.println("Lowercase: " + str.toLowerCase());
        System.out.println("Uppercase: " + str.toUpperCase());
        System.out.println("Trimmed: " + str.trim());
        System.out.println("Replaced: " + str.replace('o', '0'));
    }
}

Output:

Length: 17
Character at index 1: H
Substring (7, 12): World
Lowercase: hello, world!
Uppercase: HELLO, WORLD!
Trimmed: Hello, World!
Replaced: Hell0, W0rld!

3. String Concatenation

String concatenation is the process of joining two or more strings together. It can be done using the + operator or the concat() method.

Example

public class StringConcatenation {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";

        // Using + operator
        String result1 = str1 + ", " + str2 + "!";
        System.out.println("Using + operator: " + result1);

        // Using concat() method
        String result2 = str1.concat(", ").concat(str2).concat("!");
        System.out.println("Using concat() method: " + result2);
    }
}

Output:

Using + operator: Hello, World!
Using concat() method: Hello, World!

4. String Comparison

Strings can be compared using the equals() method, equalsIgnoreCase() method, or the compareTo() method.

Example

public class StringComparison {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "hello";
        String str3 = "Hello";

        // Using equals() method
        System.out.println("str1.equals(str2): " + str1.equals(str2));
        System.out.println("str1.equals(str3): " + str1.equals(str3));

        // Using equalsIgnoreCase() method
        System.out.println("str1.equalsIgnoreCase(str2): " + str1.equalsIgnoreCase(str2));

        // Using compareTo() method
        System.out.println("str1.compareTo(str2): " + str1.compareTo(str2));
        System.out.println("str1.compareTo(str3): " + str1.compareTo(str3));
    }
}

Output:

str1.equals(str2): false
str1.equals(str3): true
str1.equalsIgnoreCase(str2): true
str1.compareTo(str2): -32
str1.compareTo(str3): 0

5. String Formatting

String formatting allows you to create formatted strings using the String.format() method or the printf() method.

Example

public class StringFormatting {
    public static void main(String[] args) {
        String name = "John";
        int age = 30;

        // Using String.format() method
        String formattedString1 = String.format("My name is %s and I am %d years old.", name, age);
        System.out.println(formattedString1);

        // Using printf() method
        System.out.printf("My name is %s and I am %d years old.\n", name, age);
    }
}

Output:

My name is John and I am 30 years old.
My name is John and I am 30 years old.

Key Takeaways

  • Strings in Java are immutable objects that represent sequences of characters.
  • There are various methods available to manipulate strings, such as length(), charAt(), substring(), toLowerCase(), toUpperCase(), trim(), and replace().
  • Strings can be concatenated using the + operator or the concat() method.
  • String comparison can be done using equals(), equalsIgnoreCase(), and compareTo() methods.
  • String formatting can be performed using the String.format() and printf() methods.