How to convert String to String array in Java
A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An array containing strings with such a set length is indeed the String array. For converting a String to a String array, there are 5 methods.
Example:
Input: String=“Om Ram”
Output:String[]=[Om,Ram]
Here are some of them:
- Using the String.split() function
- creating loops
- Employing the Set.toArray() method
- Applying the String tokenizer
- Using the Pattern.split() method
Method 1: Using the String.split() function
Approach:
- Create a string-typed array.
- String array for splitting an array.
- String name.split() can be used to divide the provided string .
- Should print the divided string array.
StringArr.java
// This program is for converting the string to a String array
// by using the str.split() method
// import section
import java.io.*;
// Main section
public class StringArr {
// Main method of the program
public static void main(String[] args)
{
// Given input string to convert to string array
String st = "Java Programming Language";
String stArray[] = st.split(" ");
System.out.println("Given String is: " + st);
System.out.println("Converted String array : [ ");
// Iterating over the string
for (int i = 0; i < stArray.length; i++) {
// The array of elements is printed
System.out.print(stArray[i] + ", ");
}
System.out.print("]");
}
}
Output:

Method 2: By using the loops
ArrayHash.java
// This program is for converting the string to a String array
// by using the HashSet and the set classes
// import section
import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
// Main section
public class ArrayHash{
// 1st method
// converting string to string[] array
public static String[] method(Set<String> string)
{
// Create String[] of size of setOfString
String[] stringarray = new String[string.size()];
//elements are copied to set array
//by using the for loop
int position = 0;
for (String st: string) {
stringarray[position++] = st;
}
// returning the formed character array
return stringarray;
}
// 2nd Method
// Main method
public static void main(String[] args)
{
// user input
String st= "Java Programming language";
// object set_String is created
Set<String> set_string
= new HashSet<>(Arrays.asList(st));
// Printing the setOfString
System.out.println("The given String is: " + st);
// the set is converted to a string array
String[] Stringarray = method(set_string);
// printing the array consisting of strings
// by using the arrays.toString() method
System.out.println("The String array is: "
+ Arrays.toString(Stringarray));
}
}
Output:

3. Using the Set.toArray() method
Approach:
- Transform the provided string into a group of strings.
- Make a blank string array now.
- By giving a blank array of the integer type to set.toArray(), you may transform the string set to an array of strings.
- Display the string array.
Example: Stringarr.java
//This program is for converting the string to string array
//By using the method Set.toArray()
//import section
import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
// Main section
public class Stringarr {
// 1 st Method
// to convert the string to string array
public static String[] convert(Set<String> setOfString)
{
// a string String[] is created
String[] arrayString = setOfString.toArray(new String[0]);
// returning the resultant string
return arrayString;
}
// 2 Method
//Main Method
public static void main(String[] args)
{
//creating the string
String st = "Java Programming language";
//Set of string
Set<String> strings
= new HashSet<>(Arrays.asList(st));
//printing the string
System.out.println("Given String: " + st);
// Converting Set to String array
String[] stringarray = convert(strings);
// Print the arrayOfString In the print statement
// the method is used (Arrys.toString())
System.out.println("The converted String array : "
+ Arrays.toString(stringarray));
}
}
Output:

4. Using a string tokenizer
A string tokenizer is a tool that breaks up a string object into progressively smaller pieces. Tokens refer to these more compact components.
- Tokenize the supplied string.
- Make a type string array with the token counts' sizes in it.
- Should save these tokens in a string array.
- Displaying the string array in the print statement.
Example: StringToken.java
// This program is for converting the string to a String array
// by using the string tokenizer class
// importing the required packages
import java.io.*;
import java.util.StringTokenizer;
// Main section of the program
public class StringToken {
// Main method of the class
public static void main(String[] args)
{
// the integer value iss declared
// to 0
int i = 0;
// giving the user input
String st = "Java Programming Language";
// the string is divided into tokens
// using the delimiter
StringTokenizer strtokenizer
= new StringTokenizer(st);
String[] stringarray
= new String[strtokenizer.countTokens()];
// the tokens are appended to the array
while (strtokenizer.hasMoreTokens()) {
stringarray[i] = strtokenizer.nextToken();
i++;
}
//printing the given string
System.out.print("The given String :" + st);
// displaying the string array
System.out.print("\nThe String array is : [ ");
// the string array is printed
// by using the loos
for (String s: stringarray) {
System.out.print(s + " ");
}
System.out.print("]");
}
}
Output:

5. Method of using the pattern.split() function.
Using the split() function, a provided text is divided into an array by a predefined pattern. By providing a certain pattern, we may divide our string.
Approach:
- Create a pattern (REGEX)
- Then use the compilation technique to make a pattern.
- Then divide the string utilizing the pattern.
- Use split() using a particular pattern and save the results in the array.
- Print the array of strings
PatternSplit.java
// This program is for converting the string to a String array
// by using the pattern.split() method
// importing the required packages
import java.io.*;
import java.util.regex.Pattern;
// Main section
public class PatternSplit {
// Main method of the program
public static void main(String[] args)
{
// user input as the string
String s = "Java Programming Language";
// Step:1 defining the re expression
String mypattern = "\\s";
// Step2:The pattern is created by using the compile method
Pattern patt = Pattern.compile(mypattern);
// Step 3:The arry is created by using the declared pattern in above
String[] stringarray = patt.split(s);
// Printing the given string
// and also it's converted array
System.out.print("The given String is : " + s);
System.out.print("\nThe String array is : [ ");
// The String iterated using the for loop
for (int i = 0; i < stringarray.length; i++) {
// the Stringarray is printed
System.out.print(stringarray[i] + " ");
}
System.out.print("]");
}
}
Output:
