Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, you can split strings using the split()
method of the String
class. This method takes a regular expression as an argument and splits the string around matches of the given regular expression. In this tutorial, we will cover examples of using the split()
method to split strings.
Example 1: Splitting a string by a single delimiter
Suppose you have a string containing a list of words separated by a comma. You can use the split()
method to split the string into an array of words.
public class Main { public static void main(String[] args) { String text = "apple,banana,orange,grape"; // Split the string using the comma as a delimiter String[] words = text.split(","); // Print the resulting array for (String word : words) { System.out.println(word); } } }
Example 2: Splitting a string by multiple delimiters
If you have a string with multiple delimiters, you can use the split()
method with a regular expression that includes all the delimiters. For instance, if you have a string containing a list of words separated by commas and/or semicolons, you can split the string as follows:
public class Main { public static void main(String[] args) { String text = "apple;banana,orange;grape"; // Split the string using commas and semicolons as delimiters String[] words = text.split("[,;]"); // Print the resulting array for (String word : words) { System.out.println(word); } } }
Example 3: Splitting a string by whitespace
You can also use the split()
method to split a string by whitespace characters, such as spaces, tabs, and newline characters.
public class Main { public static void main(String[] args) { String text = "Hello, how are you?\nI hope you're doing well."; // Split the string using whitespace as a delimiter String[] words = text.split("\\s+"); // Print the resulting array for (String word : words) { System.out.println(word); } } }
In this example, we use the regular expression \\s+
, which matches one or more whitespace characters.
In this tutorial, we covered examples of using the split()
method in Java to split strings by different types of delimiters. The split()
method is a powerful tool for processing strings and can handle a wide range of delimiters using regular expressions.
String Splitting Methods in Java:
String text = "Java is fun";
Splitting Strings using StringTokenizer in Java:
StringTokenizer
to split strings.StringTokenizer tokenizer = new StringTokenizer(text); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); }
Java split()
Method Examples:
split()
method for string splitting.String[] words = text.split(" ");
Splitting Strings with Regular Expressions in Java:
String[] parts = text.split("\\s+"); // Split by whitespace
Java Split String by Delimiter:
String[] fragments = text.split("is");
Tokenizing and Splitting Text in Java:
StringTokenizer tokenizer = new StringTokenizer(text, "aeiou"); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); }
Splitting Strings into Arrays in Java:
String[] sentences = text.split("\\.");
Java Split String and Trim Whitespace:
String[] trimmedWords = text.split("\\s+");
Parsing CSV Files with Java Split String:
split()
method.String csvData = "John,Doe,30"; String[] csvValues = csvData.split(",");
Java Split String by Newline Character:
String[] lines = text.split("\\r?\\n");
Splitting and Extracting Substrings in Java:
String[] substrings = text.split(" ", 2);
Splitting Strings with Multiple Delimiters in Java:
String complexText = "Java;Python|C#"; String[] languages = complexText.split("[;|]");
Java Split String into Words:
String[] words = text.split("\\W+");
Common Mistakes with split()
in Java:
split()
method.String[] brokenWords = text.split(" "); // Incorrect usage