I just announced the new Spring 5 modules in REST With Spring:


This article is part of a series:
• Java String.String()
• Java String.split()
• Java String.codePointCount()
• Java String.codePointAt()
• Java String.concat()
• Java String.contains()
• Java String.copyValueOf()
• Java String.endsWith()
• Java String.format()
• Java String.getBytes()
• Java String.indexOf()
• Java String.intern()
• Java String.isEmpty()
• Java String.lastIndexOf()
• Java String.regionMatches()
• Java String.replace()
• Java String.replaceAll()
• Java String.split()
• Java String.startsWith()
• Java String.subSequence()
• Java String.substring()
• Java String.toLowerCase()
• Java String.toUpperCase()
• Java String.trim()
• Java String.valueOf()

The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings.

We can also pass a limit to the number of elements in the returned array. If we pass 0 as a limit, then the method will behave as if we didn’t pass any limit, returning an array containing all elements that can be split using the passed delimiter.

Available Signatures

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


public void whenSplit_thenCorrect() {
    String s = "Welcome to Baeldung";
    String[] expected1 = new String[] { "Welcome", "to", "Baeldung" };
    String[] expected2 = new String[] { "Welcome", "to Baeldung" };
    assertArrayEquals(expected1, s.split(" "));
    assertArrayEquals(expected2, s.split(" ", 2));


  • PatternSyntaxException – if the pattern of the delimiter is invalid.
@Test(expected = PatternSyntaxException.class)
public void whenPassInvalidParameterToSplit_thenPatternSyntaxExceptionThrown() {
    String s = "Welcome*to Baeldung";
    String[] result = s.split("*");

I just announced the new Spring 5 modules in REST With Spring: