Wednesday, 20 July 2016

Splitting a String using split() method in Java

String provides a split method in order to split the string into one or more substring based on the given regular expression.

split() method has 2 variants -

  • split(String regex) - Splits this string around matches of the given regular expression. The array returned by this method contains each substring of this string that matches the given expression.The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
  • split(String regex, int limit) - The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

Example Code

  1. If you have a string where one (or more) spaces are used and you want to split it around those spaces.
    public class StringSearch {
     public static void main(String[] args) {
      String str1 = "split example    program";
      String[] strArray = str1.split("\\s+");
      System.out.println("" + strArray.length);
      for(String w : strArray){
       System.out.println("words - " + w);
      }
     }
    }
    

    Output

    3
    words - split
    words - example
    words – program
    
  2. If you have a date in dd/mm/yyyy format and you want to split it into day, month and year.
    public class StringSearch {
     public static void main(String[] args) {
      String date = "20/01/2016";
      String[] dateArr = date.split("/");
      System.out.println("" + dateArr.length);
      System.out.println("day " + dateArr[0] + " Month " + dateArr[1] + " Year " + dateArr[2]);
     }
    }
    

    Output

    3
    day 20 Month 01 Year 2016
    

Using with limit argument

Suppose you just want the day part of the date then you can use the split() method which also passes limit as argument -

public class StringSearch {

 public static void main(String[] args) {
  String date = "20/01/2016";
  String[] dateArr = date.split("/", 2);
  System.out.println("" + dateArr.length);
  System.out.println("day " + dateArr[0]);

 }

}
Output
2
day 20

That's all for this topic Splitting a String using split() method in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related topics

  1. String in Java
  2. String comparison in Java
  3. String charAt() and subString() methods in Java
  4. Searching within a String using indexOf(), lastIndexOf() and contains() methods
  5. How to find all the permutations of the given String
  6. Java String interview questions

You may also like -

>>>Go to Java Basics page

1 comment:

  1. The behavior of split method is changed a bit since Java 8 release. You can add that aspect also. Otherwise the post is really awesome.

    ReplyDelete