Introduction: Java String

In Java, a String is a class that represents a sequence of characters. It is part of the java.lang package and it is one of the most commonly used classes in Java. The String class is immutable, meaning that once a String object is created, its content cannot be changed.

Java is unique in supporting operator overloading exclusively for the String class. We can concatenate two strings using the + operator, as demonstrated by the expression “a” + “b” resulting in “ab.”

For effective string manipulation, Java provides two valuable classes: StringBuffer and StringBuilder. These classes allow for dynamic modification of string content, making them mutable compared to the immutable nature of regular String objects.

Save $100 in the next
5:00 minutes?

Register Here

Different Ways to Create String

Several approaches exist for creating a string object in Java, and some commonly used methods are outlined below.

1. Using string literal:This method involves directly assigning a string value to a variable using double quotes.


String str1 = "Hello, World!";

2. Using new keyword: This method employs the new keyword to explicitly instantiate a new string object.


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

Java String Methods

Java provides a rich set of methods in the String class to manipulate and work with strings. Here are some commonly used methods:

Save $100 in the next
5:00 minutes?

Register Here

Method 1: length(): Returns the length (number of characters) of the string.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int length = str.length(); // Returns 13
        System.out.println(length);
    }
}

OUTPUT:

13M

Method 2: charAt(): Returns the character at the specified index.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        char firstChar = str.charAt(0); // Returns 'H'
        System.out.println(firstChar);
    }
}

OUTPUT:

H

Method 3:  substring(): Returns a substring within the specified range.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String sub1 = str.substring(7);       // Returns "World!"
        String sub2 = str.substring(0, 5);    // Returns "Hello"
        System.out.println(sub1);
        System.out.println(sub2);
    }
}

OUTPUT:

World!
Hello

Method 4: concat(): Concatenates the specified string to the end of the current string.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String result1 = str.concat(", Java!"); // Returns "Hello, World! Java!"
        String result2 = str + " Java!";        // Returns "Hello, World! Java!"
        System.out.println(result1);
        System.out.println(result2);
    }
}

OUTPUT

Hello, World!, Java!
Hello, World! Java!

Method 5: equals(): Compares the content of two strings for equality.

Example:


class Test {
        String str = "Hello, World!";
        boolean isEqual = str.equals("Hello, World!"); // Returns true
        boolean isEqualIgnoreCase = str.equalsIgnoreCase("hello, world!"); // Returns true
        System.out.println(isEqual);
        System.out.println(isEqualIgnoreCase);
    }
}

OUTPUT:

true
true

Method 6:  indexOf(): Returns the index of the first occurrence of the specified substring.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int indexOfComma = str.indexOf(","); // indexOfComma is 5
        System.out.println(indexOfComma);
    }
}

OUTPUT:

5

Method 7: startsWith(): Checks if the string starts with the specified prefix.

Example:



class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean startsWithHello = str.startsWith("Hello"); // startsWithHello is true
        System.out.println(startsWithHello);
    }
}

OUTPUT:

True

Save $100 in the next
5:00 minutes?

Register Here

Method 8: endsWith(): Checks if the string ends with the specified suffix.

Example:


class Test {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean endsWithWorld = str.endsWith("World!"); // endsWithWorld is true
        System.out.println(endsWithWorld);
    }
}

OUTPUT:

True

Conclusion

Overall, the String class is fundamental in Java for text manipulation, and its immutability ensures data integrity.

Save $100 in the next
5:00 minutes?

Register Here