Monday, September 5, 2022

Java split() Method - Splitting a String

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. Here passed regex "\\s+" means one or more 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 Java split() Method - Splitting a String. 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 equals(), compareTo(), startsWith() Methods
  2. Java String substring() Method - Getting Substring
  3. String Vs StringBuffer Vs StringBuilder in Java
  4. Find All Permutations of a Given String Java Program
  5. Java String Interview Questions And Answers

You may also like-

  1. Count Number of Words in a String Java Program
  2. Java Program to Find The Longest Palindrome in a Given String
  3. Multi-Catch Statement in Java Exception Handling
  4. final Vs finally Vs finalize in Java
  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 With Examples

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