DEV Community

Cover image for Java String Methods With Examples ?
Arul .A
Arul .A

Posted on

Java String Methods With Examples ?

  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.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

6.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

7.palindrome check
check if string equals its reverse.

Example :

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

8.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

9.substring() :

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

Eaxmple :

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

10.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

11.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 (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Avoid giving ? in Title. Integer.ParseInt is not String method