split() Method
Tags: java, beginners, codenewbie, computerscience
Published: 2021-08-22 16:12:12

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 :-**
```Java
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.

```Java
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](https://www.tutorialspoint.com/compile_java_online.php)***
![java-split()-0.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629532082803/8Zf7MAOGS.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-

```Java
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](https://www.tutorialspoint.com/compile_java_online.php)***
![java-split()-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629534796554/Xnr3v5Qrw.png)
> Note that this takes a regular expression, so remember to escape [special characters](https://www.regular-expressions.info/characters.html) 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.

```Java
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](https://www.tutorialspoint.com/compile_java_online.php)***
![java-split()-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629535753629/auAe6Umiz.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*