JAVA Syllabus
QUESTIONS & ANSWERS

Explain all methods available in String class in JAVA?

The String class in Java provides many methods that can be used to manipulate and work with strings. Here is a brief overview of some of the most commonly used methods:

  1. charAt(int index): Returns the character at the specified index in the string.

  2. length(): Returns the length of the string.

  3. substring(int beginIndex): Returns a substring of the original string, starting from the specified index.

  4. substring(int beginIndex, int endIndex): Returns a substring of the original string, starting from the specified begin index and ending at the specified end index.

  5. toLowerCase()Returns a new string with all the characters converted to lowercase.

  6. toUpperCase(): Returns a new string with all the characters converted to uppercase.

  7. equals(Object obj): Compares the string to the specified object for equality.

  8. equalsIgnoreCase(String str): Compares the string to another string, ignoring case differences.

  9. indexOf(int ch): Returns the index of the first occurrence of the specified character in the string.

  10. indexOf(int ch, int fromIndex): Returns the index of the first occurrence of the specified character, starting from the specified index.

  11. indexOf(String str)Returns the index of the first occurrence of the specified substring in the string.

  12. indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified substring, starting from the specified index.

  13. replace(char oldChar, char newChar): Returns a new string with all occurrences of the specified character replaced with another character.

  14. replace(charSequence target, charSequence replacement): Returns a new string with all occurrences of the specified substring replaced with another substring.

  15. split(String regex): Splits the string into an array of substrings, using the specified regular expression as the delimiter.

  16. trim(): Returns a new string with leading and trailing white space removed.

These are just a few of the many methods available in the String class in Java. Other methods include startsWith, endsWith, concat, compareTo, and many more.

Here's an example program in Java that demonstrates some of the methods available in the String class:

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        // charAt method
        char firstChar = str.charAt(0);
        System.out.println("First character: " + firstChar);

        // length method
        int strLength = str.length();
        System.out.println("String length: " + strLength);

        // substring method
        String subStr = str.substring(7);
        System.out.println("Substring from index 7: " + subStr);

        String subStr2 = str.substring(7, 12);
        System.out.println("Substring from index 7 to 12: " + subStr2);

        // toLowerCase method
        String lowerCaseStr = str.toLowerCase();
        System.out.println("Lowercase string: " + lowerCaseStr);

        // toUpperCase method
        String upperCaseStr = str.toUpperCase();
        System.out.println("Uppercase string: " + upperCaseStr);

        // equals method
        boolean isEqual = str.equals("Hello, World!");
        System.out.println("Is string equal to 'Hello, World!': " + isEqual);

        // equalsIgnoreCase method
        boolean isEqualIgnoreCase = str.equalsIgnoreCase("hello, world!");
        System.out.println("Is string equal to 'hello, world!': " + isEqualIgnoreCase);

        // indexOf method
        int index = str.indexOf('W');
        System.out.println("Index of 'W': " + index);

        int index2 = str.indexOf("World");
        System.out.println("Index of 'World': " + index2);

        // replace method
        String replacedStr = str.replace('o', 'x');
        System.out.println("String with 'o' replaced with 'x': " + replacedStr);

        String replacedStr2 = str.replace("World", "Universe");
        System.out.println("String with 'World' replaced with 'Universe': " + replacedStr2);

        // split method
        String[] splitStr = str.split(", ");
        System.out.println("Split string: ");
        for (String s : splitStr) {
            System.out.println(s);
        }

        // trim method
        String trimmedStr = "   Hello, World!   ".trim();
        System.out.println("Trimmed string: " + trimmedStr);
    }
}
25/03/2023, 6:46 am Read : 333 times