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

Java Split String

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.

  1. String Splitting Methods in Java:

    • Overview of various methods to split strings in Java.
    String text = "Java is fun";
    
  2. Splitting Strings using StringTokenizer in Java:

    • Using StringTokenizer to split strings.
    StringTokenizer tokenizer = new StringTokenizer(text);
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
    }
    
  3. Java split() Method Examples:

    • Utilizing the split() method for string splitting.
    String[] words = text.split(" ");
    
  4. Splitting Strings with Regular Expressions in Java:

    • Splitting using regular expressions for flexibility.
    String[] parts = text.split("\\s+"); // Split by whitespace
    
  5. Java Split String by Delimiter:

    • Splitting strings using a specific delimiter.
    String[] fragments = text.split("is");
    
  6. Tokenizing and Splitting Text in Java:

    • Combining tokenization and splitting for advanced text processing.
    StringTokenizer tokenizer = new StringTokenizer(text, "aeiou");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
    }
    
  7. Splitting Strings into Arrays in Java:

    • Splitting strings into arrays for easy manipulation.
    String[] sentences = text.split("\\.");
    
  8. Java Split String and Trim Whitespace:

    • Removing leading and trailing whitespace after splitting.
    String[] trimmedWords = text.split("\\s+");
    
  9. Parsing CSV Files with Java Split String:

    • Parsing CSV files using the split() method.
    String csvData = "John,Doe,30";
    String[] csvValues = csvData.split(",");
    
  10. Java Split String by Newline Character:

    • Splitting strings based on newline characters.
    String[] lines = text.split("\\r?\\n");
    
  11. Splitting and Extracting Substrings in Java:

    • Extracting substrings after splitting.
    String[] substrings = text.split(" ", 2);
    
  12. Splitting Strings with Multiple Delimiters in Java:

    • Handling multiple delimiters for complex splitting.
    String complexText = "Java;Python|C#";
    String[] languages = complexText.split("[;|]");
    
  13. Java Split String into Words:

    • Extracting words from a sentence.
    String[] words = text.split("\\W+");
    
  14. Common Mistakes with split() in Java:

    • Addressing common pitfalls and mistakes with the split() method.
    String[] brokenWords = text.split(" "); // Incorrect usage