coderz.py

Keep Coding Keep Cheering!

Some examples of string class methods

java thread class

The methods listed below are some of the most frequently used String class methods in Java.

charAt() method:

The charAt() function in a string returns the character at the specified index.

class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello";
        char result = myStr.charAt(0);
        System.out.println(result);
    }
}
Output:
H
equalsIgnoreCase() method:

equalsIgnoreCase() checks for equality between two Strings while ignoring their case.

        class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello",str="Coder";
        boolean result = myStr.equalsIgnoreCase(str);
        System.out.println(result);
    }
}
Output:
false
indexOf() method:

  The index of the first occurrence of a substring or a character is returned by the indexOf() method.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.indexOf("e", 5));
    }
}
Output:
10
length() method:

The length() function of a String returns the number of characters in it.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.length());
    }
}
Output:
43
replace() method:

The replace() method in a string replaces all occurrences of a character with a new character.

public class Demo {
    public static void main(String[] args) {   
        String str = "coderzpy";
        System.out.println(str.replace('c','C'));
    }
}
Output:
Coderzpy
substring() method:

The substring() method returns a part of the string.

public class Demo {
    public static void main(String[] args) {
        String str = "ABCDEFGHIJKLMNO";
        System.out.println(str.substring(4));
        System.out.println(str.substring(4,7));        
    }
}
Output:
EFGHIJKLMNO
EFG
valueOf() method:

String class uses an overloaded version of valueOf() method for all primitive data types and for type Object.

public class Demo {
    public static void main(String[] args) {
         int num = 115;
         String s1 = String.valueOf(num);    //converting int to String
         s1+= "hello";
         System.out.println(s1);
         System.out.println("type of num : "+s1.getClass().getName());   
    }
}
Output:
115hello
type of num : java.lang.String
trim() method:

This method returns a string that has been stripped of any leading and trailing whitespaces.

public class Demo {
    public static void main(String[] args) {
        String str = "   hello coderzpy  ";
        System.out.println(str.trim());   
    }
}
Output:
hello coderzpy
endsWith() method:

the endsWith() method is used to determine whether the string ends with the specified suffix. If the suffix matches the string, it returns true; otherwise, it returns false.

public class Demo {
    public static void main(String[] args) {
        String a="Hello welcome to Coderzpy.com";  
        System.out.println(a.endsWith("m"));  
        System.out.println(a.endsWith("com"));       
    }
}
    
Output:
true
true

Note: also read about the String class methods in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement