Java Program to Convert String to String Array
Last Updated :
19 May, 2022
Given a String, the task is to convert the string into an Array of Strings in Java. It is illustrated in the below illustration which is as follows:
Illustration:
Input: String : "Geeks for Geeks"
Output: String[]: [Geeks for Geeks]
Input: String : "A computer science portal"
Output: String[] : [A computer science portal]
Methods:
They are as follows:
- Using str.split() method
- Using loops
- Using Set.toArray() method
- Using String tokenizer
- Using pattern.split() method
Lets us now discuss every method in depth implementing the same to get a better understanding of the same. They are as follows:
Method 1: Using str.split() method
Approach:
- Create an array with string type.
- Split the given string using string_name.split().
- Store splitted array into string array.
- Print the above string array.
Example
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
String str = "Geeks for Geeks" ;
String strArray[] = str.split( " " );
System.out.println( "String : " + str);
System.out.println( "String array : [ " );
for ( int i = 0 ; i < strArray.length; i++) {
System.out.print(strArray[i] + ", " );
}
System.out.print( "]" );
}
}
|
Output
String : Geeks for Geeks
String array : [
Geeks, for, Geeks, ]
Method 2: Using loops
Approach:
- Get the set of strings.
- Create an empty string array
- Use advanced for loop, copy each element of set to the string array
- Print the string array.
Example:
Java
import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
class GFG {
public static String[] method(Set<String> string)
{
String[] string_array = new String[string.size()];
int index = 0 ;
for (String str : string) {
string_array[index++] = str;
}
return string_array;
}
public static void main(String[] args)
{
String str = "Geeks for Geeks" ;
Set<String> string_set
= new HashSet<>(Arrays.asList(str));
System.out.println( "String: " + str);
String[] String_array = method(string_set);
System.out.println( "Array of String: "
+ Arrays.toString(String_array));
}
}
|
Output
String: Geeks for Geeks
Array of String: [Geeks for Geeks]
Method 3: Using Set.toArray() method
Approach:
- Convert the given string into set of string.
- Now create an empty string array.
- Convert the string set to Array of string using set.toArray() by passing an empty array of type string.
- Print the array of string.
Example:
Java
import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString
= setOfString.toArray( new String[ 0 ]);
return arrayOfString;
}
public static void main(String[] args)
{
String str = "Geeks for Geeks" ;
Set<String> string
= new HashSet<>(Arrays.asList(str));
System.out.println( "String: " + str);
String[] string_array = convert(string);
System.out.println( "String array : "
+ Arrays.toString(string_array));
}
}
|
Output
String: Geeks for Geeks
String array : [Geeks for Geeks]
Method 4: Using String tokenizer
String tokenizer helps to split a string object into smaller and smaller parts. These smaller parts are known as tokens.
- Tokenize the given string
- Create an array of type string with the size of token counts.
- Store these tokens into a string array.
- Print the string array.
Example:
Java
import java.io.*;
import java.util.StringTokenizer;
public class GFG {
public static void main(String[] args)
{
int i = 0 ;
String str = "Geeks for Geeks" ;
StringTokenizer str_tokenizer
= new StringTokenizer(str);
String[] string_array
= new String[str_tokenizer.countTokens()];
while (str_tokenizer.hasMoreTokens()) {
string_array[i] = str_tokenizer.nextToken();
i++;
}
System.out.print( "String :" + str);
System.out.print( "\nString array : [ " );
for (String string : string_array) {
System.out.print(string + " " );
}
System.out.print( "]" );
}
}
|
Output
String :Geeks for Geeks
String array : [ Geeks for Geeks ]
Method 5: Using pattern.split() method
The purpose of this pattern.split() method is to break the given string into an array according to a given pattern. We can split our string by giving some specific pattern.
Approach:
- Define pattern (REGEX)
- Then create a pattern using the compilation method
- Then split the string using pattern.split() with a specific pattern and store it in the array.
- Print the string array
Example:
Java
import java.io.*;
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args)
{
String str = "Geeks for Geeks" ;
String my_pattern = "\\s" ;
Pattern pattern = Pattern.compile(my_pattern);
String[] string_array = pattern.split(str);
System.out.print( "String : " + str);
System.out.print( "\nString array : [ " );
for ( int i = 0 ; i < string_array.length; i++) {
System.out.print(string_array[i] + " " );
}
System.out.print( "]" );
}
}
|
Output
String : Geeks for Geeks
String array : [ Geeks for Geeks ]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...