Java-Split Method

Split Method

You can also use the split method to split a string into an array of String objects based on delimiters that match a regular expression. One common way to do that is to simply create a custom class of characters that can be used for delimiters. For example, in the following program, a string is split into words marked by colons, comma,or slash.


Program Source

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Javaapp {
    public static void main(String[] args) {
        Pattern pat = Pattern.compile("[:,/]");
        String str[] = pat.split("Java:C++,C/PHP");
        for(int i=0; i<str.length; i++)
            System.out.println("str["+i+"] : "+str[i]);

Leave a Comment