Splitting strings is a common operation in Java, especially when processing structured data. In this article, we will split each string in an array using a given delimiter and reconstruct the array with the resulting parts.
Understanding the Problem
We are given an array of strings, where each string contains multiple words or elements separated by a delimiter (e.g., comma, space, hyphen). Our goal is to split each string by the delimiter and store all resulting parts in a new array.
Example:
Each word pair is split by "-"
, and all resulting words are stored in a new array.
INPUT:
String[] words = {“apple-banana”, “grape-mango”, “peach-orange”};
String delimiter = “-“;
OUTPUT:
[“apple”, “banana”, “grape”, “mango”, “peach”, “orange”]
Approach to Solve the Problem
- Iterate through each string in the array.
- Use Java’s
split()
method to break each string using the delimiter. - Store all parts in a new list or array.
Java Program for Splitting Strings by a Delimiter (Using split() with an ArrayList)
The Java program efficiently concatenates an array of strings using a specified delimiter by leveraging StringBuilder to minimize unnecessary string creation.
package com.javacodepoint.stringarray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SplitStringsByDelimiter {
// Method to split each string by the delimiter and reconstruct the array
public static String[] splitStrings(String[] words, String delimiter) {
List<String> resultList = new ArrayList<>();
for (String word : words) {
String[] parts = word.split(delimiter); // Split the string
resultList.addAll(Arrays.asList(parts)); // Add parts to the list
}
return resultList.toArray(new String[0]); // Convert list to array
}
public static void main(String[] args) {
// Example array of strings
String[] words = { "apple-banana", "grape-mango", "peach-orange" };
String delimiter = "-";
// Split strings and reconstruct the array
String[] result = splitStrings(words, delimiter);
System.out.println("Reconstructed Array: " + Arrays.toString(result));
}
}
OUTPUT:
Reconstructed Array: [apple, banana, grape, mango, peach, orange]
Alternative Approach: Using Java Streams
Java Streams API provides a functional way to split and flatten arrays.
import java.util.Arrays;
import java.util.stream.Stream;
public class SplitUsingStreams {
public static void main(String[] args) {
String[] words = {"apple-banana", "grape-mango", "peach-orange"};
String delimiter = "-";
String[] result = Arrays.stream(words)
.flatMap(word -> Arrays.stream(word.split(delimiter)))
.toArray(String[]::new);
System.out.println("Reconstructed Array: " + Arrays.toString(result));
}
}
Conclusion
This article explored different ways to split an array of strings by a delimiter and reconstruct it. We covered:
- Using
split()
with an ArrayList (Efficient & Readable). - Using Java Streams (
flatMap()
) (Concise & Functional).
Understanding these methods helps in text processing, data manipulation, and coding interviews.
You can learn the Top 20 string array programs for interview preparation (Click here).