How to Split a String in Java with Delimiter? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma delimiter in Java using the split() method: Java // Java program to split a string with a Comma delimiter public class SplitString { public static void main(String[] args) { // Declare a string with comma delimiter String s = "Hello,Good,Morning"; // Split the string using comma as the delimiter String[] b = s.split(","); // Print each fruit on a new line for (String a : b) { System.out.println(a); } } } OutputHello Good Morning Other Ways to Split a String with Delimiter in JavaApart from the simple method demonstrated above, there are few more methods which we can use to split the string in Java.Using String Tokenizer classThe String Tokenizer class in Java is present in java.util package. The main purpose of String Tokenizer is to divide or break the given strings into several tokens based upon the delimiter.Example: Java // Java program to split a string with delimeter using String Tokenizer Class import java.util.StringTokenizer; public class StringTokenizerExample { public static void main(String[] args) { // Creating a new string tokenizer object with given input string StringTokenizer st = new StringTokenizer("Java Programming"); System.out.println("No. of tokens:" +st.countTokens()); // Iterate through token and print each token while (st.hasMoreTokens()) { // Get next token and print it with space System.out.println("" +st.nextToken()); } } } OutputNo. of tokens:2 Java Programming Using Scanner ClassThe Scanner class in Java allow a user to read input from the keyboard. Scanner class is used to tokenize an input string. Java Scanner class is present in java.util package.Example: Java // Java program to split a string with delimeter using String Scanner Class import java.util.Scanner; public class Test { public static void main(String[] args) { String s = "Geeks For Geeks"; // Create a Scanner object with input string Scanner sc = new Scanner(s); System.out.println(""); // Iterate through token and print each token while(sc.hasNext()) { System.out.println(sc.next()); } } } OutputGeeks For Geeks Comment More infoAdvertise with us Next Article How to Split a String in Java with Delimiter? D d_singh Follow Improve Article Tags : Java Java Programs Java-Strings Java-StringTokenizer Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads How to Convert a String to ArrayList in Java? Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet 1 min read How to Split Strings Using Regular Expressions in Java? Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split Stri 2 min read How to Split a String into Equal Length Substrings in Java? In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.Example:In the 3 min read How to Convert a Comma-Separated String into an ArrayList in Java? In Java, to convert a comma-separated string into ArrayList, we can use the split() method to break the string into an array based on the comma delimiter. Another method we can also use Java Streams for a functional approach to achieve the same result.Example:Input: "Hello,from,geeks,for,geeks"Outpu 2 min read Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil 2 min read Like