java string split function

By | October 16, 2019

In automation interviews or in real time projects, its common to encounter a situation where we need to perform few string split operations. In this article, you are going to learn how to split a string with different examples.

String split(String regex)

String split(String regex, int limit)

1st argument: Here regex is separator. Based on this factor a string will be split. For example in the string “selenium java tutorials”, all three words are separated by a space. So if we give space as regex in the function, we will get an array of strings.

2nd argument: limit is used to define the maximum number of splits.

Example 1:

Output: [selenium, java, tutorials]

Example 2:

If the separator is ‘_’, we will get the output as below.

Output: [automation, tutorials, website, for, beginners]

Example 3:

Separator is space in the below scenario.

Output: [This, is, selenium, tutorial]

 

Example 4:

Both separator and limit are used in the below scenario.

Output: [splitting, string, in_java]

Example 5:

Output: [se, enium99tutoria, s]

Example 6:

Output:

[Automation: Selenium and Java]
[Automation, Selenium and Java]

 

Leave a Reply

Your email address will not be published. Required fields are marked *