DEV Community

Vidya
Vidya

Posted on

String Methods

1. trim()
Removes extra spaces only at the beginning and end.

Example


         String s=" Admin123 ";
         System.out.println(s.trim()); // output:"Admin123"
Enter fullscreen mode Exit fullscreen mode

2.equalsIgnoreCase()
Compares two strings ignoring uppercase/lowercase.

Example

"Vijay".equalsIgnoreCase("vijay"); // true
Enter fullscreen mode Exit fullscreen mode

3.equals() vs ==
== checks memory reference(same object or not).
equals() checks actual content/value.

Example

          String s1="java";
          String s2=new String("java");

          s1==s2  // output:false
          s1.equals(s2) // output:true
Enter fullscreen mode Exit fullscreen mode

4.contains
checks if a substring exists inside a string.Returns true or false.

Example

        String email="test@gmail.com";
        email.contains("@"); // output:true

Enter fullscreen mode Exit fullscreen mode

5.Integer.parseInt()
converts String to integer.

Example

         int num=Integer.parseInt("1234");
         System.out.println(num + 10); // output:1244

Enter fullscreen mode Exit fullscreen mode

6.split() + loop(count word)
split sentences into words and count occurrences.

Example

       String s="java is easy java is powerful";
       String[] arr=s.split(" ");
       int count=0;
       for(String word :arr){
          if(word.equals("java"))
          count++;
        }
        System.out.println(count); // output: 2
Enter fullscreen mode Exit fullscreen mode

7.Reverse String(without StringBuilder)
Traverse from end to start.

Example

           String s="java";
           String rev="";
           for(int i=s.length()-1;i>=0;i--){
              rev+=s.charAt(i);
           }
           System.out.println(rev); // output:avaj
Enter fullscreen mode Exit fullscreen mode

8.palindrome check
check if string equals its reverse.

**Example**
Enter fullscreen mode Exit fullscreen mode
        public class PalindromeString {
             public static void main(String[] args) {
        String str = "madam";
        String rev = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }

        if (str.equals(rev))
            System.out.println("Palindrome");
        else
            System.out.println("Not Palindrome");
    }
}                            // output:palindrome
Enter fullscreen mode Exit fullscreen mode

9.Remove duplicates
Remove repeated characters.maintains unique characters only.

** Example**

        public class Duplicate1 {
          public static void main(String[] args) {
           String name="aabbccdd";
           String copy="";
           for(int i=0;i<name.length();i++) {
               if(copy.indexOf(name.charAt(i))==-1) {
                   copy+=name.charAt(i);
               }
           }
           System.out.println(copy);
       }
}                         // output:abcd
Enter fullscreen mode Exit fullscreen mode

10.substring()

       Extract part of a string.
Enter fullscreen mode Exit fullscreen mode

Example

      String log="Error:File not Found";
      System.out.println(log.substring(7)); // output:file not found

Enter fullscreen mode Exit fullscreen mode

11.replace()

     Replaces old word with new word.
Enter fullscreen mode Exit fullscreen mode

Example

     String s="I love java";
     System.out.println(s.replace("java", "Spring Boot"));        
      //output:I love Spring Boot
Enter fullscreen mode Exit fullscreen mode

12.matches()
used for pattern checking(like mobile number).**

Example

  String mobile="9876543210";
  mobile.matches("\\d{10}"); // output:true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)