Sunday, March 31, 2013

How to Split String in Java using Regular Expression

String class provides split() method to split String in Java, based upon any delimiter, e.g. comma, colon, space or any arbitrary method. split() method splits string based on delimiter provided, and return a String array, which contains individual Strings. Actually, split() method takes a regular expression, which in simplest case can be a single word. split() is also overloaded method in java.lang.String class and its overloaded version takes a limit parameter which is used to control how many times pattern will be applied during splitting process. if this limit is positive n, then pattern will be applied at most n-1 times, if it's negative or zero than split operation is applied any number of time. For example, if we split String "First,Second,Third" on comma and provide limit as 2 then pattern will run one time, and split() will return String array with 2 Strings, "First" and "Second,Third". Since this method accepts a Java regular expression, it throws PatternSyntaxException, if syntax of regular expression is invalid.


String Split Example in Java

String Split Example with regular expression in JavaLet's see couple of String Split example in Java. In first example, we will split a colon separated String on colon as delimiter, and in second example we will split comma separated String. In third example, we will split String using regular expression, which actually splits a String on space as delimiter. \\s is a regular expression to find spaces. \s is actually used to match any white space character including new line, tab, form feed e.g. \t, \n, \r and \f. \\ is to escape backward slash which is a escape characters in Java. If you are familiar with Java Regular Expression, than split(regex) method is similar to Pattern.compile(regex).split().

package test;

/**
 * Java program to split String in Java using delimiter.
 * This String Split example shows how to split String in Java using comma, colon and space as delimiter.
 * Splitting String by space uses regular expression \\s to split String.
 *
 * @author http://java67.blogspot.com
 */
public class StringSplitExample {
 
    public static void main(String args[]) {

      //Splitting colon separated String in Java
      String colon = "Java:J2EE:JavaFX:JavaME";
     
      String[] strings = colon.split(":");
     
      System.out.println("Original Colon Separated String : " + colon);
      System.out.println("String splitted using Split() method of java.lang.String in Java");
     
      for(String str : strings){
          System.out.println(str);
      }
     
      //Splitting comma separated String in Java
      String comma = "Android,Windows 8,iOS,Symbian";
      String[] strs = comma.split(",");
      System.out.println("Original comma separated String : " + comma);
      System.out.println("Splitting String using split() method in Java");
    
      for(String str: strs){
          System.out.println(str);
      }
     
      //Splitting String on space as delimiter in Java
      String space = "String Split Example in Java";
      String[] words = space.split("\\s");
      System.out.println("Space separated String before split : " + space);
      System.out.println("Space separated String after split");
     
      for(String word: words){
          System.out.println(word);
      }
    
    } 
   
}
Output:
Original Colon Separated String : Java:J2EE:JavaFX:JavaME
String splitted using Split() method of java.lang.String in Java
Java
J2EE
JavaFX
JavaME
Original comma separated String : Android,Windows 8,iOS,Symbian
Splitting String using split() method in Java
Android
Windows 8
iOS
Symbian
Space separated String before split : String Split Example in Java
Space separated String after split
String
Split
Example
in 
Java

That's it on How to split String in Java using delimiter and regular expression. split(regex) method is very powerful, and all it's power comes from regular expression. Though it's not required to know regular expression to split String in Java, but if you know regex, it is only going to help you.

Related Java String tutorials from Java67 Blog

3 comments:

  1. Remember, when you split String, it always create a new String object, because String is Immutable in Java.

    ReplyDelete
  2. how to split a word hithis into two words in java like hi this.

    ReplyDelete
  3. How to split two string having any operator between them
    "23+56","45-89","5656*4554"

    ReplyDelete

Java67 Headline Animator