How to Convert a String to String Array in Java?8 May 2025 | 6 min read In Java, a String is an object that represents the sequence of characters. In order to use Strings, we need to import the String class defined in java.lang package. The String array is an array of strings with a fixed length. In this section, we will learn how to convert String into String array. String: In Java, a string is an object that represents a sequence of characters. Strings are immutable, meaning their values cannot be changed once they are created. The String class in the java.lang package provides various methods for string manipulation. String Array: A string array is an array of strings, where each element of the array holds a string value. It allows developers to store multiple strings in a single data structure. Converting String to String ArrayThere are four ways to convert a String into String array in Java:
Using String.split() MethodThe String.split() method is used to split the string into individual strings entities based on the given delimiter (whitespace or other symbols). We can store these entities directly in a string array. Syntax Let's consider the following example where we use String.split() method to convert a string into a string array. TestSplitMethod.java Output: ![]() Explanation The Java code that is provided shows how to use the split() function to turn a string into a string array with whitespace acting as the delimiter. It uses the split() method to divide a string variable called str into distinct words depending on whitespace after initializing it with a specific sentence. A for loop is used to print the substrings that are produced to the console after they have been saved in a string array called strArray. This indicates that the string has been successfully divided into distinct substrings based on whitespace since it enables each word from the original string to be displayed on a different line. Example 2: In the following example, we have converted the string to a string array based on the delimiter that is a , (comma). TestSplitMethod2.java Output: ![]() Explanation The Java code snippet that is provided defines a class called TestSplitMethod2, which has a main() function. In this procedure, "Hello,have,a,nice,day" is used to initialise the string variable commaSeparatedStr. The string is divided into separate substrings using the split() method with a comma as the delimiter. The substrings are then saved in a string array called strArray. Each substring in strArray is sent to the console as it is iterated through using a for loop. Consequently, every word from the initial string separated by commas appears on a different line. Using Pattern.split() MethodThe Pattern.split() method is used to split the string into an array of strings with the help of regular expression (pattern) as the delimiter. In order to use the method, we need to import the Pattern class in our Java code as: Syntax Let's consider the following example where we split a string into an array by using the delimiter as whitespace. SplitMethodOfPatternClass.java Output: ![]() Explanation The Java code snippet that is provided defines a class called TestSplitMethod2, which has a main() function. In this procedure, "Hello,have,a,nice,day" is used to initialise the string variable commaSeparatedStr. The string is divided into separate substrings using the split() method with a comma as the delimiter. The substrings are then saved in a string array called strArray. Each substring in strArray is sent to the console as it is iterated through using a for loop. Consequently, every word from the initial string separated by commas appears on a different line. Example 2: We can also split a string into an array by using any string or pattern as a delimiter. Here, we have used the delimiter #a1. SplitMethodOfPatternClass2.java Output: ![]() Explanation This snippet of Java code divides a string str into separate substrings according to the delimiter #a1 by using the Pattern.split() method. The code initialises the string str with the value "Hello #a1Ben #a1how #a1are #a1you?" after importing the Pattern class. A pattern object, ptr, is constructed using Pattern, and an empty string array, strArray, is declared.utilise the delimiter pattern #a1 in compile(). Next, ptr is subjected to the split() method, which divides str into substrings according to #a1 and stores the outcome in strArray. The above example splits the single string into five separate strings based on the delimiter #a1. The parts of the string that matched the delimiter are not included in the array. Using String[ ] ApproachWe can simply convert string to string array by using the string index [ ]. Here, we pass the string to String [ ] {}. Syntax Consider the following example where we have used the String[] to convert a string into a string array. StrToStrArray.java Output: ![]() Explanation "Converting string to string array using String[]" is the value that this Java program initializes in the string variable str. The string str is the only element in the string array strArray that is created at that point. Arrays.toString(), which turns the array into a string representation, is then used to print the contents of the string array to the console. Using toArray() MethodWe can also convert String to String array by using the toArray() method of the List class. It takes a list of type String as the input and converts each entity into a string array. Syntax Consider the following example where we have converted the list of strings into a string array. StringListtoArray.java Output: ![]() Explanation The ArrayList and List classes are imported first in this Java programme from the java.util package. Next, an ArrayList called list is created to hold strings. Four strings, "Hello", "Welcome", "To", and "Tutorial", are added to the list. The list's size is then calculated using size(), and a string array called strArray is initialised with the same size as the list. Next, it uses the toArray() method to transform the contents of the list to a string array. |
The noneMatch() method in Java's Stream API is an essential function employed to evaluate whether none of the elements in a given stream satisfy a particular condition. It is particularly useful when we need to prove that none of the items in a collection match...
11 min read
In Java, evaluating mathematical expressions can sometimes be a complex and error-prone task. Manually parsing and calculating expressions can lead to tedious and lengthy code. To simplify this process, we can leverage the power of EvalEx (Evaluate Expression) Java, a lightweight Java library that provides an...
5 min read
Java, being one of the most widely used programming languages, provides a robust exception handling mechanism to help developers identify and handle runtime errors effectively. Exception messages play a crucial role in this process, as they provide valuable information about the nature and cause of the...
4 min read
In Java, variables and constants are basic concepts that play an important role in managing and manipulating data in a program. In this section, we will explore the key differences between variables and constants in Java. Variables in Java Variables are containers that store data values. It...
7 min read
Concurrency is a fundamental aspect of modern software development, and Java provides several mechanisms to handle concurrent tasks efficiently. Two commonly used synchronization tools in Java are CyclicBarrier and CountDownLatch. Despite their similar-sounding names, these classes serve distinct purposes in managing concurrent operations. In this section,...
4 min read
When developing software applications, especially command-line programs, it's common to use a menu-driven approach to provide users with a clear and organized way to interact with the application. Java, as a versatile and widely used programming language, offers the perfect platform to implement menu-driven programs. In...
7 min read
In the realm of digital entertainment, games have always held a special place, captivating audiences with their immersive experiences and engaging gameplay. One technology that has played a significant role in the development of countless games is Java. Known for its versatility, portability, and extensive libraries,...
4 min read
The Java synchronization class known as Exchanger is the most captivating. Through the creation of a synchronization point, it makes the exchange of elements between two threads easier. The data transfer between two threads is made simpler. The way it works is that it just...
3 min read
String reversal is a common programming problem where a given string needs to be reversed such that the last character of the string becomes the first character, and vice versa. However, when preserving white spaces, the order of the white spaces should be maintained in the...
9 min read
? In this section, we will learn different ways to open the Java control panel. Also, we will discuss the uses of the Java Control Panel. Java Control Panel The Java Control Panel is a multipurpose control panel. It allows us to view and set a wide range of...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India