
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we split a string on a fixed character sequence in java?
In this article, we will learn how to split a string on a fixed character sequence in Java. We have multiple ways to split a string in Java. Some of them are as follows:
- Using the split() method of the String class.
- Using the substring() method of the String class.
- Using the StringTokenizer class.
Let's discuss each of them in detail one by one.
Using the split() method of the String class
The split() method of the String class accepts a delimiter (in the form of a string), divides the current String into smaller strings based on the delimiter, and returns the resulting strings as an array. If the String does not contain the specified delimiter, this method returns an array that contains only the current string.
For example, if you pass a single space " " as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.
If the String does not contain the specified delimiter, this method returns an array containing the whole string as an element.
Splitting the string on a fixed character sequence
To split a String into an array of strings, each time a particular String occurs:
- Read the source string.
- Invoke split() method by passing the desired String as a delimiter.
- Print the resultant array.
Example
The Following Java program reads the contents of a file into a String and splits it using the split() method with another string as a delimiter.
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class SplitExample { public static void main(String args[]) throws FileNotFoundException { Scanner sc = new Scanner(new File("D:\sample.txt")); StringBuffer sb = new StringBuffer(); String input = new String(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(input); } String source = sb.toString(); String result[] = source.split(" to "); System.out.print(Arrays.toString(result)); } }
When the above code is executed, it produces the following output:
[Tutorials Point originated from the idea that there exists a class of readers who respond better, on-line content and prefer, learn new skills at their own pace from the comforts of their drawing rooms.]
Using the substring() method of the String class
The substring() method is used for extracting a substring from a string. We need to pass the starting and ending indices of the substring to be extracted. The starting index is inclusive, and the ending index is exclusive. Let's look at the steps to split a string using the substring() method:
- Read the source string.
- Find the index of the delimiter in the string.
- Use the substring() method to extract the string before and after the delimiter.
- Repeat the process until the delimiter is not found.
Example
Following is the program that has a string, which we will split on a fixed character sequence using the substring() method.
public class SplitStringEx{ public static void main(String args[]){ String str = "Tutorials point is a learning platform"; String delimiter = "is"; int index = str.indexOf(delimiter); while (index != -1){ String beforeDelimiter = str.substring(0, index); String afterDelimiter = str.substring(index + delimiter.length()); System.out.println("Before delimiter: " + beforeDelimiter); System.out.println("After delimiter: " + afterDelimiter); str = afterDelimiter; index = str.indexOf(delimiter); if (index == -1) { System.out.println("Final string: " + str); }else { System.out.println("Delimiter found at index: " + index); } } } }
When the above code is executed, it produces the following output:
Before delimiter: Tutorials point After delimiter: a learning platform Delimiter found at index: -1 Final string: a learning platform
Using the StringTokenizer class
We can use methods of the StringTokenizer class to split a string on a fixed character sequence. This class breaks a string into tokens and also provides methods to access the tokens. We can use this to our advantage to split a string on a fixed character sequence.
Let's look at the steps to split a string using the StringTokenizer class:
- Read the source string.
- Use the StringTokenizer class to break the string into tokens.
- Print the tokens.
Example
Following is the program that has a string, which we will split on a fixed character sequence using the StringTokenizer class.
import java.util.StringTokenizer; public class SpiltStringExmpl{ public static void main(String args[]){ String str = "Tutorials point is a learning platform"; String delmetr = "is"; StringTokenizer st = new StringTokenizer(str, delmetr); while(st.hasMoreTokens()){ String token = st.nextToken(); System.out.println("Token: " + token); if (st.hasMoreTokens()) { System.out.println("Delimiter found at index: " + str.indexOf(delmetr)); } else { System.out.println("Final string: " + token); } } } }
The following will be the output of the above program:
Token: Tutorials point Delimiter found at index: 10 Token: a learning platform Final string: a learning platform Delimiter found at index: -1