split() Method

split() Method

ยท

3 min read

Hello all๐Ÿ˜€ I hope you are doing well. This is going to be a very short and very useful article. In this article I'll be writing about split() method in Java. This is a one of the most useful methods.

Let's begin...

split()

This is a method of Java String class. This method splits a given string around matches of the given regular expression. This method allows you to break a string based on specific Java string delimiter. More precisely this method splits string against given regular expression and returns a char array.

May be a few of you might have heard the word Delimiter for the first time, so, delimiter is one or more characters that separate text strings. Some very common delimiters are comma(,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ ).

General Syntax :-

string_name.split("delimter");

This ๐Ÿ‘† is the general form in which you can use split() method. Let's see an example to make it clear.

public class Demo {
    public static void main(String[] args)
    {
        String str = "Hello, I'm a string";
        String[] arrOfStr = str.split(",");
        for (String a : arrOfStr) {
            System.out.println(a);
        }
    }
}

You can run your code online here java-split()-0.png Here in this example you can see we the split string at delimiter that is at the comma.

There are two ways to split string :-

1. split(String regex)

This method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. By default limit is 0. It returns an array of strings calculated by splitting the given string. Above example is also a type of this but we will see a more advanced example-

public class Demo {
    public static void main(String[] args)
    {
        String str = "Hello|I'm|your;String|exa#mple";
        String[] arrOfStr = str.split("[|;#]");
        for (String a : arrOfStr) {
            System.out.println(a);
        }
    }
}

You can run your code online here java-split()-1.png

Note that this takes a regular expression, so remember to escape special characters if necessary

2. split(String regex, int length)

Parameters for this are: regex (the delimiting regular expression) and limit (controls the number of times the pattern is applied and therefore affects the length of the resulting array). This returns the array of strings counted by splitting this string around matches of the given regular expression.

public class Demo {
    public static void main(String[] args) {
        String str = "8989-01214-1521654-544744";
        String[] arrOfStr = str.split("-", 2);
        for (String a : arrOfStr ) {
            System.out.println(a);
        }
    }
}

You can run your code online here java-split()-2.png

The PatternSyntaxException will be thrown if the provided regex's syntax is invalid.

Okay so that's enough for now there is lot more to learn about splitting, this article was to give you basic idea how split works.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com