
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do we split a string on a fixed character sequence in java?
The split() method of the String class accepts a delimiter (in the form of the 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 which contains only the current string.
For example if you pass 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 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
Following Java program reads the contents of a file into a Sting 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)); } }
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.]
- Related Articles
- How do I split a string, breaking at a particular character in JavaScript?
- How do we split a string with any whitespace chars as delimiters using java?
- How do we find out if first character of a string is a number in java?
- How do we use a delimiter to split string in Python regular expression?
- How can we split a string by sentence as a delimiter in Java?
- How to split a string in Java?
- Split a string in Java
- How do we compare String in Java
- In how many ways we can convert a String to a character array using Java?
- In how many ways can we split a string in JavaScript?
- How do we create a string from the contents of a file in java?
- How do we check if a String contains a substring (ignoring case) in Java?
- Split a string around a particular match in Java
- How do you convert a string to a character array in JavaScript?
- How do I split a multi-line string into multiple lines?
