Useful String Methods in Java

 Java String provides multiple methods to work with. Here we'll discuss some of the most useful String methods and these are as follows:

  • int length()
  • String concat(String str)
  • boolean equals(Object obj)
  • boolean equalsIgnoreCase(String str)
  • String toLowerCase()
  • String toUpperCase()
  • char charAt(int index)
  • String substring(int start, [int end])
  • boolean contains(Charsequence c)
  • String[] split(String regex, [int limit])


Let's discuss each method with an example:

    1. length() method:
                This method returns the actual length of the string.
                For Example:

                        String str = "hello";
                      System.out.println("length of the string is: " + str.length());   
            
                Output: length of the string is: 5

    2. concat(String str):
                This method returns a new String by concatenating the string passed as an argument. 
                For Example:

                        String str = "hello";
                      String newString = str.concat(" world");
                      System.out.println(newString);  

                Output: hello world

    3. equals(Object obj):
                This method compares string values and returns a boolean i.e. if values are same it will return true otherwise returns false.

                Note: equals() method performs case sensitive comparisons.

                For Example:

                        String str1 = "hello";
                      String str2 = "HELLO";

                      System.out.println(str1.equals(str2));

                Output: false //Since both values are different

    4. equalsIgnoreCase(String str):
                This method compares two strings ignoring case differences. If the strings are equal, it returns true otherwise false.

                For Example:

                        String str1 = "hello";
                      String str2 = "HELLO";

                      System.out.println(str1.equalsIgnoreCase(str2));

                Output: true 

    5. toLowerCase( ):
                It returns a new string in lowercase.
                For Example:

                           String str = "hEllO";
                        System.out.println(str.toLowerCase( ));

                Output: hello

    6. toUpperCase( ):
                It returns a new string in uppercase.
                For Example:

                           String str = "hEllO";
                        System.out.println(str.toUpperCase( ));

                Output: HELLO

    7. charAt(int index):
                This methods accepts an index as a parameter and returns a character value at the given index number.
                
                Note: If the index is negative or greater than the length of the string itself, then this method will throw StringIndexOutOfBoundsException.
                For Example:

                            String str = "think design and create";
                         System.out.println("Character at index 10 is: " + str.charAt(10)); 
    
                Ouput: Character at index 10 is: g

    8. substring(int beginIndex, [int endIndex]):
                This method returns a new string that is a substring of a string. The substring starts from the  index position(beginIndex) till the end of a string/endIndex.
                 
                 Here second parameter is optional, if not provided then this method returns a substring starting from the beginIndex till the end of the string.
                For Example:
                        
                            String str = "think design and create";
                         String val_one = str.substring(6);
                         System.out.println(val_one);  //Output: design and create
             
                         String val_two = str.substring(0, 4); //It will not include the char at index 4
                         System.out.println(val_two); //Output: thin


    9. contains(Charsequence c):
                Java contains methods searches the charsequence within a string, if found it returns a true value otherwise false.
                For Example:
            
                            String str = "hello world";
                         System.out.println(str.contains("hello"));

                Output: true

    10. split(String regex, [int limit]):
                This method splits a given string based on the given regex and returns an array.
              
                 Note: Here second parameter is optional, it defines the number of strings in the returned    array. If its value is zero or not provided, then array will contain all the matching string.
                For Example:

                            String str = "think-design-and-create";
                         String[] csv = str.split("-");  //csv : ["think", "design", "and", "create"]

                         String values = str.split("-", 2) //values: ["think", "design-and-create"]


   To understand the difference between String and String literal: click here
                            
                    
                
















Comments

Popular Posts