Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java split() Method

Java split() Method

Java String ClassJava String class


The split() method splits the string based on matches of the given regular expression.

Note: Escape characters such as . , $, | and * must be preceded by \.

**Note:** For multiple delimiters, you can use | as the connector.

Syntax


public String[] split(String regex, int limit)

Parameters

Return Value

String array.

Example

Example

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

String

str

=

new

String

(

"

Welcome-to-Runoob

"

)

;

System

.

out

.

println

(

"

"

)

;

for

(

String

retval

:

str

.

split

(

"

"

)

)

{

System

.

out

.

println

(

retval

)

;

}

System

.

out

.

println

(

"

"

)

;

System

.

out

.

println

(

"

"

)

;

for

(

String

retval

:

str

.

split

(

"

"

,

2

)

)

{

System

.

out

.

println

(

retval

)

;

}

System

.

out

.

println

(

"

"

)

;

String

str2

=

new

String

(

"

www.runoob.com

"

)

;

System

.

out

.

println

(

"

Return value for escape characters :

"

)

;

for

(

String

retval

:

str2

.

split

(

"

\

.

"

,

3

)

)

{

System

.

out

.

println

(

retval

)

;

}

System

.

out

.

println

(

"

"

)

;

String

str3

=

new

String

(

"

acount=? and uu =? or n=?

"

)

;

System

.

out

.

println

(

"

Return value for multiple delimiters :

"

)

;

for

(

String

retval

:

str3

.

split

(

"

and|or

"

)

)

{

System

.

out

.

println

(

retval

)

;

}

}

}

The execution result of the above program is:


Delimiter return value:
Welcome
to
Runoob

- Separator sets the number of splits return:
Welcome
to-Runoob

Escape character return value :
www
runoob
com

Multiple delimiter return values :
acount=? 
 uu =? 
 n=?

Java String ClassJava String class