In this post of Splitting a String in Java using a delimiter We will learn how to split a string based on provided delimiter.In many applications, you get data in a text file that is separated by a comma (,) symbol or a pipe (|) symbol or a tab icon (/ t) or can be any delimiter. Now, if you want to make a quick split around that different symbol, you can easily do this by using the split () method of String class. In this post we are going to see a java program to split a string.
You might be interested to read:Count number of words in a string java
In this java program ,We are going see how to split input string based on delimiter like colon(:) or pipe(|) or comma(,) or tab(\t) etc..
But you may modify this program to split your input string for any other delimiter as per your choice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.kkjavatutorials.client; /** * @author KK JavaTutorials *In this java program we will see how to Split a String by provided any delimiter */ public class SplitStringTest { public static void main(String[] args) { String inputTextCommaDelimiter = "Sean,Martin,KK,Jacky,Jancy,Hema"; splitStringCommaDelimiter(inputTextCommaDelimiter); String inputTextTabDelimiter = "Sean Martin KK Jacky Jancy Hema"; splitStringTabDelimiter(inputTextTabDelimiter); String inputTextPipeDelimiter = "Sean|Martin|KK|Jacky|Jancy|Hema"; splitStringPipeDelimiter(inputTextPipeDelimiter); } //Method to split input string based on pipe(|) delimiter private static void splitStringPipeDelimiter(String inputTextPipeDelimiter) { System.out.println("Input String splitted based on pipe(|) delimiter:"); if(inputTextPipeDelimiter == null || inputTextPipeDelimiter.isEmpty()){ System.out.println("Enter valid String.."); return; } //As we knnow that pipe(|) is also used in conditions like OR (||) //so that is a special symbol and needs to be escaped. String delimeter = "\\|"; String[] splitStringArray = inputTextPipeDelimiter.split(delimeter); for (String s : splitStringArray) { System.out.println(s); } } //This method shows how to split input string based on tab(\t) delimiter private static void splitStringTabDelimiter(String inputTextTabDelimiter) { System.out.println("Input String splitted based on tab("+"\\t"+")"+" delimiter:"); if(inputTextTabDelimiter == null || inputTextTabDelimiter.isEmpty()){ System.out.println("Enter valid String.."); return; } //Here delimiter is tab(\t) String delimeter = "\t"; String[] splitStringArray = inputTextTabDelimiter.split(delimeter); for (String s : splitStringArray) { System.out.println(s); } } //Method to split input string based on comma(,) delimiter private static void splitStringCommaDelimiter(String inputTextCommaDelimiter) { System.out.println("Input String splitted based on comma(,) delimiter:"); if(inputTextCommaDelimiter == null || inputTextCommaDelimiter.isEmpty()){ System.out.println("Enter valid String.."); return; } //Here delimiter is comma(,) String delimeter = ","; String[] splitStringArray = inputTextCommaDelimiter.split(delimeter); for (String s : splitStringArray) { System.out.println(s); } } } |
If you run this program in your IDE you get blow output::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Input String splitted based on comma(,) delimiter: Sean Martin KK Jacky Jancy Hema Input String splitted based on tab(\t) delimiter: Sean Martin KK Jacky Jancy Hema Input String splitted based on pipe(|) delimiter: Sean Martin KK Jacky Jancy Hema |
Would you like to watch on YouTube:
Splitting a String in Java using a delimiter
That’s all in this post How to split a String in Java using a delimiter in java.
You May Also Like:
Reverse a string in Java (6 Different Ways)
Count number of words in a string java
Count total number of times each character appears in the string in java
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
Java Program to find the frequency of each character in String ?
If you have any feedback or suggestion please feel free to drop in below comment box.