All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Convert String To String Array In Java

Last Updated : Mar 11, 2024

How To Convert String To String Array In Java

In this article we will show you the solution of how to convert string to string array in java, as usual first we need to import package in case you are using eclipse then all program will import by itself during creation.

When thinking to convert as string array then you to divide the string words into separate values to store as array. So we used split() method to split them by word-wise and we achieving the result.

Step By Step Guide On How To Convert String To String Array In Java :-

The main class ‘StrArr_prgm’ we provided with main method definition. There you have to define the words and ensure whether your string has more than two words to get accurate result.

Now we know to use split() function to divide the string into words of array. But you not sure how to use split with the array for that reason we leads you with sample program follow below given code.

//String To String Array
package java_prgms;
class StrArr_prgm
{
 public static void main(String args[])
    {
        String s="Hi Welcome Everyone";
        String[] sarr=null;
        sarr=s.split(" ");
        for(int i=0;i<sarr.length;i++)
        System.out.println(sarr[i]);
     }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class ‘StrArr_prgm’ we providing public static void main method definition. Within that you have to define sting variable so we declared string variable ‘s’ with text value ‘Hi Welcome Everyone’.
  3. Next we defined empty array ‘sarr’ with value ‘null’. To separate the long text to word-wise you need to binds that string variable ‘s’ with split() method. And remember within split brackets you have to put double quotes with single space then only you get correct output.
  4. Because that denotes the space between words and depend on that calculation it proceeding separation process.
  5. Then result stored on array variable ‘sarr’ and defined for loop with array length condition. This loop will continue until reaching the length of array.
  6. Within loop we printing each values stored on array variable.
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to do convert string to string array in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac StrArr_prgm.java’.

After successful compilation you need to execute ‘java StrArr_prgm‘ line for gets the output.

Then you can see output 'Hi, Welcome, Everyone’. On eclipse just press of run button then you will get output inside terminal.

I hope this article on how to convert string to string array in java helps you and the steps and method mentioned above are easy to follow and implement.