In this article, we will understand with a Java program on how to split a String based on delimiter using Java 1.8 version
Already in one of the previous article, we discussed how to split a String based on some delimiter using earlier versions of Java like 5 or 7, etc.
Split a String based on delimiter :
- split() method of String
- Returns String[] Arrays after splitting the invoking string using specified delimiter (or regex)
- There are 2 variants of split() method,
- split(String regex); – Returns string[] arrays after splitting the invoking string using specified regex
- split(String regex, int limit); -Returns string[] arrays after splitting the invoking string using specified regex with specified threshold limit
- For both variants, throws PatternSyntaxException if the specified regex is invalid or simply not compliable
- Delimiter used for the below illustration are,
- Dot or period (.)
- Hyphen (-)
- Space ( )
- Forward–slash (/)
- Pipe (|)
- Colon (:)
- Semi–colon (;)
- Comma (,)
SplitStringBasedOnDelimiter.java
package in.bench.resources.java8.string.methods;
import java.util.Arrays;
import java.util.stream.Stream;
public class SplitStringBasedOnDelimiter {
public static void main(String[] args) {
// 1. test string 1
String str1 = "in.bench.resources.java8.string.methods";
System.out.println("Original String :- " + str1);
// 1.1 split String on the basis of Dot/Period (.) delimiter
String[] dotDelimiterArr = Stream
.of(str1)
.map(str -> str.split("\\."))
.findFirst()
.get();
System.out.println("Dot/Period splitted String :- " + Arrays.toString(dotDelimiterArr));
// 2. test string 2
String str2 = "24-July-1985";
System.out.println("\nOriginal String :- " + str2);
// 2.1 split String on the basis of Hyphen (-) delimiter
String[] hyphenDelimiterArr = Stream
.of(str2)
.map(str -> str.split("\\-"))
.findFirst()
.get();
System.out.println("Hyphen splitted String :- " + Arrays.toString(hyphenDelimiterArr));
// 3. test string 3
String str3 = "Java is super cool language";
System.out.println("\nOriginal String :- " + str3);
// 3.1 split String on the basis of space ( ) delimiter
String[] spaceDelimiterArr = Stream
.of(str3)
.map(str -> str.split("\\s"))
.findFirst()
.get();
System.out.println("Space splitted String :- " + Arrays.toString(spaceDelimiterArr));
// 4. test string 4
String str4 = "16/02/1987";
System.out.println("\nOriginal String :- " + str4);
// 4.1 split String on the basis of forward-slash (/) delimiter
String[] fwdSlashDelimiterArr = Stream
.of(str4)
.map(str -> str.split("\\/"))
.findFirst()
.get();
System.out.println("Forward-slash splitted String :- " + Arrays.toString(fwdSlashDelimiterArr));
// 5. test string 5
String str5 = "Mango|Apple|Melon|Grape|Cherry";
System.out.println("\nOriginal String :- " + str5);
// 5.1 split String on the basis of pipe (|) delimiter
String[] pipeDelimiterArr = Stream
.of(str5)
.map(str -> str.split("\\|"))
.findFirst()
.get();
System.out.println("Pipe splitted String :- " + Arrays.toString(pipeDelimiterArr));
// 6. test string 6
String str6 = "20:35:24";
System.out.println("\nOriginal String :- " + str6);
// 6.1 split String on the basis of colon (:) delimiter
String[] colonDelimiterArr = Stream
.of(str6)
.map(str -> str.split("\\:"))
.findFirst()
.get();
System.out.println("Colon splitted String :- " + Arrays.toString(colonDelimiterArr));
// 7. test string 7
String str7 = "Simran;Sneha;Meena;Soundarya;Roja;Nagma;Kushbhu";
System.out.println("\nOriginal String :- " + str7);
// 7.1 split String on the basis of semi-colon (;) delimiter
String[] semiColonDelimiterArr = Stream
.of(str7)
.map(str -> str.split("\\;"))
.findFirst()
.get();
System.out.println("Semi-Colon splitted String :- " + Arrays.toString(semiColonDelimiterArr));
// 8. test string 8
String str8 = "1,SJ,Admin,BRN,800,2014,2022";
System.out.println("\nOriginal String :- " + str8);
// 8.1 split String on the basis of comma (,) delimiter
String[] commaDelimiterArr = Stream
.of(str8)
.map(str -> str.split("\\,"))
.findFirst()
.get();
System.out.println("Comma splitted String :- " + Arrays.toString(commaDelimiterArr));
}
}
Output:
Original String :- in.bench.resources.java8.string.methods
Dot/Period splitted String :- [in, bench, resources, java8, string, methods]
Original String :- 24-July-1985
Hyphen splitted String :- [24, July, 1985]
Original String :- Java is super cool language
Space splitted String :- [Java, is, super, cool, language]
Original String :- 16/02/1987
Forward-slash splitted String :- [16, 02, 1987]
Original String :- Mango|Apple|Melon|Grape|Cherry
Pipe splitted String :- [Mango, Apple, Melon, Grape, Cherry]
Original String :- 20:35:24
Colon splitted String :- [20, 35, 24]
Original String :- Simran;Sneha;Meena;Soundarya;Roja;Nagma;Kushbhu
Semi-Colon splitted String :- [Simran, Sneha, Meena, Soundarya, Roja, Nagma, Kushbhu]
Original String :- 1,SJ,Admin,BRN,800,2014,2022
Comma splitted String :- [1, SJ, Admin, BRN, 800, 2014, 2022]
Related Articles:
- Java 8 – How to get a specific character from String ?
- Java 8 – How to check whether particular word/letter/sub-string is present in the String ?
- Java 8 – How to check whether particular String endsWith specific word/letter ?
- Java 8 – How to check whether particular String startsWith specific word/letter ?
- Java 8 – How to check whether a String is empty or not ?
- Java 8 – How to get length of a String ?
- Java 8 – How to convert a String into char[] Arrays ?
- Java 8 – How to convert a String into UpperCase String ?
- Java 8 – How to convert a String into LowerCase String ?
- Java 8 – How to remove leading and trailing whitespaces in a String ?
- Java 8 – How to replace a String with another String ?
- Java 8 – How to split a String based on delimiter ?
- Java 8 – How to convert primitive data-types into String ?
- Java 8 – How to join String[] Arrays elements using different delimiter ?
- Java 8 – How to join List of String elements using different delimiter ?
- Java 8 – How to find 1st and last index of particular character/substring in a String ?
- Java 8 – How to get hashCode of a String ?
- Java 8 – How to get sub-string from a String ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!