Learn Selenium and Java for Selenium

Saturday, 7 July 2018

Strings in Java : Part 6

String Methods

Exited about the next method that we are going to explain is Split() method in string. It is commonly asked method in the interviews and it is used most frequently in the Automation projects.

Split() method in short returns the array of the string based on the delimiter provided in the method.

So let us understand the basic syntax and usage of the split method


  • String split()
    • The split() method split the string into the arrays of sub string based on the separator specified
  • split() method is available with 2 different variables which are
    • String array[]= str.split("/")- It will return the array of strings whenever a '/' is encountered in string. 
    • String array[]= str.split("/,2")- It will return array of only 2 strings even after the '/' is present in string more than 2 times.


Let us understand the split() method using the programs



public class Main{  
public static void main(String args[]){  

String s="Learn/Selenium/Automation";  

   String[] a=s.split("/");//   

      System.out.println(a[0]);
      System.out.println(a[1]);
      System.out.println(a[2]);
  
 }
}  

The output of the above program is 

Learn
Selenium
Automation

                    Now Lets try the other variant of the split() method

public class Main{  
public static void main(String args[]){  

String s="Learn/Selenium/Automation/Strings";  

   String[] a=s.split("/,2");//   

      System.out.println(a[0]);
      System.out.println(a[1]);
 
  
 }
}  

The output of the above program is 

Learn
Selenium/Automation/String


Hope you have understood the Post if there is any doubt please feel free to add the comments section we would answer your queries :)

0 comments:

Post a Comment

Popular Posts

Recent Posts



Newsletter

Join our newsletter to get secret tips directly in your inbox!


*We Hate Spam!

Powered by Blogger.