Monday, August 5, 2019

Splitting a String - Java split() Method

split method in Java String class is used to split the string into one or more substring based on the given regular expression.

Java split() method has 2 variants-

  • split(String regex)- Splits this string around matches of the given regular expression. This method returns an array containing each substring of this string that matches the given expression.
  • 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.

String's split() method examples

  1. If you have a string where one (or more) spaces are used and you want to split this String 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("Words in array- " + strArray.length);
      for(String w : strArray){
       System.out.println("words - " + w);
      }
     }
    }
    

    Output

    Words in array- 3
    words - split
    words - example
    words – program
    
  2. If you have a date in dd/mm/yyyy format and you want to split this date String 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 split() method 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 - Java split() Method. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. String Comparison in Java
  2. String charAt() And substring() Methods in Java
  3. String Vs StringBuffer Vs StringBuilder in Java
  4. How to Find All The Permutations of The Given String
  5. Java String Interview Questions

You may also like-

  1. Count Number of Words in a String - Java Program
  2. How to Find The Longest Palindrome in The Given String
  3. Multi-Catch Statement in Java Exception Handling
  4. final Vs finally Vs finalize
  5. Java pass by value or pass by reference
  6. finalize method in Java
  7. Difference between HashMap and HashTable in Java
  8. Callable and Future in Java concurrency

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