Methods in Java String Class

Java String class is method rich. Let’s see, available methods in Java string class. We have a lot of methods in Java, we can perform operations on the string such as trimming, concatenating, converting, comparing, replacing strings etc.

 

Methods in Java String Class

1. Java String Concat() method – This method is used to contact specified string at the end of source String.

Example –

String str1=”Easy”;  

str1.concat(“Bix”);  

System.out.println(str1);  

output –  EasyBix

 

2. Java String charAt() method –  This method return the charter at the given index. Index starts from 0.

Example –

String str=”EASY”;  

char c=str.charAt(3);

System.out.println(c);

Output – Y

 

Selenium Testing Video Tutorial:

 

3. Java String contains() method – This method searches the target string in the source String. It returns result true and false. True if the string is present otherwise return false.

Example –

String str= “Hello, I am on EasyBix”;  

System.out.println(name.contains(“am on”));  

Output – true

 

4. Java String endsWith() method – This method verify that source string ends with given suffix or not and return the result in true/false.

Example –

String str= “Hello, I am on EasyBix”;  

System.out.println(name.endsWith(“Bix”));  

Output – true

 

5. Java String indexof() method – This method return the index value of given character/String in the source String.

Example –

String str=”Hello, I am on EasyBix “;    

int index1=str1.indexOf(“on”);

System.out.println(index1);

Output – 12

 

6. Java String isEmpty() method – This method verify the String, that it is empty or not. This method returns the result in true or false.

Example –

String str=” “;    

System.out.println(str.isEmpty());

Output – true

 

7. Java string replaceAll() method – This method replace all the characters with the given character and return the modified string.

Example –

String str=”SeleniumBix”;  

String replaceString=str.replaceAll(“e”,”i”);

System.out.println(replaceString);  

Output – SiliniumBix

 

8. Java String subString() method – This method return the part of String. In this method, we pass the index number as argument and It will return the String from the given Index number.

Example –

String str=”SeleniumBix”;  

System.out.println ( str.subString (2,6) ); 

System.out.println ( str.subString(2) );  

Output – leniu

                leniumBix

 

9. Java String lowerCase() & upperCase() methods – Methos lowerCase() is used to convert the given String into lowercase string and upperCase() is used to convert the String into upperCase String.

Example –

String str=”SELENIUM”; 

String str1 = “bix”; 

System.out.println ( str.lowerCase() ); 

System.out.println ( str1.upperCase() );  

Output – selenium

               BIX

1 Comment
  1. Pratik Jain 7:58 PM / June 25, 2017 - Reply

    String str1=”Easy”;
    str1.concat(“Bix”);
    System.out.println(str1);
    output – EasyBix

    This example is wrong..

    Correct will be

    String str1=”Easy”;
    String str2 =str1.concat(“Bix”);
    System.out.println(str2);
    output – EasyBix

Leave a Reply