String methods – split()

In this post, we will discuss about the string methods – Split().

This method is used to split the string on the basis of given regular expression in arguments. It will return a String array.

  1. String[] split(String regex)
  2. String[] split(String regex,int limit)

String[] split(String regex):

This will split the String on the basis of given¬†regular expression. Let’s look at the example below. Where we are splitting an Ip address on the basis of dot(.) and another string is splitted with space(” “).

Output:
arr[0] : 124
arr[1] : 100
arr[2] : 125
arr[3] : 10
**********************************
This
is
testingpool.com

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

This method has one more parameter called ‘limit’, which decides the length of array returned after splitting the String.Let’s understand with the example below.

Output:
Length of first array : 2
Length of Second array : 3
124
100.125.10
************************
124
100
125.10
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Print Friendly, PDF & Email
String methods - replace
String methods - matches and regionMatches
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...