Thursday 16 March 2017

A program on split() method in java

Splitting a String is the most day to day required activity for a programmer. You must have a good grasp of the process of splitting a String.

We have two widely used possible ways of splitting a String in java.

  • split(), &
  • StringTokenizer()
Here we are going to show you example of the most preferred of the two methods. 

String : Learning|Java|Is|Fun
Delimmiter: |  

public class SplitString {

       public static void main(String[] args) {

              String s1 = "Learning|Java|Is|Fun";
             
              //String[] s2 = s1.split("\\|");
              String[] s2 = s1.split(Pattern.quote("|")); //you need to                                                                             importjava.util.regex.Pattern
              int i = 0;

              while (i < s2.length) {

                     System.out.println(s2[i]);
                     i++;

              }
             
       }
}


You may also want to know about:

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.