Java String split()

19 Mar 2025 | 3 min read

The java string split() method splits this string against given regular expression and returns a char array.


Internal implementation


Signature

There are two signature for split() method in java string.


Parameter

regex : regular expression to be applied on string.

limit : limit for the number of strings in array. If it is zero, it will returns all the strings matching regex.


Returns

array of strings


Throws

PatternSyntaxException if pattern for regular expression is invalid


Since

1.4


Java String split() method example

The given example returns total number of words in a string excluding space only. It also includes special characters.

Output

java
string
split
method
by
TpointTech

Java String split() method with regex and length example

Output

returning words:
welcome 
to 
split 
world
returning words:
welcome to split world
returning words:
welcome 
to split world

Java String split() method with regex and length example 2

Here, we are passing split limit as a second argument to this function. This limits the number of splitted strings.

Output

Returning words:
Tpoin
Tech
Split array length: 2