Convert String to String Array in Java

In this tutorial, we will learn how to Convert String to String Array in Java.

While working on Strings, we may need to convert a big String into an array of Strings. The String class in Java provides a method split() to split the String into an array of Strings. The split() method splits the string as per the regular expression passed to it.

The split() method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The string “This is String”, for example, yields the following result with the expression:

Regex    Result
  ” ”        { “This”, “is”, “String” }

Parameters:
regex – the delimiting regular expression

Returns:
the array of strings computed by splitting this string using the regular expression

Throws:
PatternSyntaxException – if the regular expression’s syntax is invalid

Let’s see how to convert String to String array using the split() method with an example.

Example:

Output:

In the above program, we have used split() method to convert String to String Array.

Other Tutorials:

Related posts

Leave a Comment