Convert Java String to Array

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to convert a string to an array using Java code. We will be using the split() method of the String class to split a string based on specified delimiter and return an array. By the end of this lab, you will have a clear understanding of how to convert a string to an array in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117962{{"`Convert Java String to Array`"}} java/classes_objects -.-> lab-117962{{"`Convert Java String to Array`"}} java/class_methods -.-> lab-117962{{"`Convert Java String to Array`"}} java/modifiers -.-> lab-117962{{"`Convert Java String to Array`"}} java/oop -.-> lab-117962{{"`Convert Java String to Array`"}} java/identifier -.-> lab-117962{{"`Convert Java String to Array`"}} java/arrays -.-> lab-117962{{"`Convert Java String to Array`"}} java/data_types -.-> lab-117962{{"`Convert Java String to Array`"}} java/for_loop -.-> lab-117962{{"`Convert Java String to Array`"}} java/operators -.-> lab-117962{{"`Convert Java String to Array`"}} java/output -.-> lab-117962{{"`Convert Java String to Array`"}} java/strings -.-> lab-117962{{"`Convert Java String to Array`"}} java/variables -.-> lab-117962{{"`Convert Java String to Array`"}} java/string_methods -.-> lab-117962{{"`Convert Java String to Array`"}} java/system_methods -.-> lab-117962{{"`Convert Java String to Array`"}} end

Create the Java file

Firstly, create a Java file in the ~/project directory using the following command:

touch ~/project/StringToArray.java

Define the class and the main method

Define the class and the main method by adding the following code to your file:

public class StringToArray {
    public static void main(String[] args) {

    }
}

Define the string to be converted

Define the string that we want to convert by adding the following code inside the main method:

String message = "StudyTonight.com is a technical portal";

Convert the string to an array

Now, let's convert the string to an array. We will use the split() method of the String class with space as a delimiter. Add the following code inside the main method:

String[] stringArray = message.split(" ");

Print the array elements

To make sure that the conversion was successful, let's print the elements of the array. Add the following code inside the main method:

for (int i = 0; i < stringArray.length; i++) {
    System.out.println(stringArray[i]);
}

Compile and run the code

Save the file by pressing Ctrl+O and then exit by pressing Ctrl+X. Now, compile and run the code using the following commands:

javac StringToArray.java
java StringToArray

You should see the output as:

StudyTonight.com
is
a
technical
portal

Convert a URL string to an array

Let's take another example where we have a URL string and want to get it as an array. In this case, we will use '/' as a delimiter. Add the following code inside the main method:

String url = "StudyTonight.com/tutorial/java/string";
String[] urlArray = url.split("/");

Print the URL array elements

To confirm the conversion, letโ€™s print the elements of the URL array. Add the following code inside the main method.

System.out.println("\nURL elements:");
for (int i = 0; i < urlArray.length; i++) {
    System.out.println(urlArray[i]);
}

Compile and run the code

Save the file by pressing Ctrl+O and then exit by pressing Ctrl+X. Now, compile and run the updated code using the following commands:

javac StringToArray.java
java StringToArray

You should see the output as:

StudyTonight.com is a technical portal
StudyTonight.com
is
a
technical
portal

URL elements:
StudyTonight.com
tutorial
java
string

Summary

In this lab, we have learned how to convert a string to an array using the split() method of the String class in Java. We first defined the string that we want to convert. Then, we used the split() method with space as the delimiter to convert the string to an array. Finally, we compiled and ran the code to get the output. We also learned how to convert a URL string to an array by using '/' as a delimiter.

Other Java Tutorials you may like