Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Split the String in Java?

Split String Example

Condition for Splitting a String in Java

  • Description: In Java, splitting a string can be done using the split() method of the String class. This method divides a string into an array of substrings based on a specified delimiter, which can be a regular expression. The split() method returns an array of strings, with each element containing a part of the original string separated by the delimiter. If the delimiter appears multiple times, the resulting array will contain empty strings for consecutive delimiters. For example, splitting a string by spaces or commas results in substrings without the delimiter itself. Additionally, specifying a limit can control the number of substrings returned, especially when there are multiple delimiters. For instance, using split(" ") splits a sentence into individual words.
Sample Source Code
  • # SplitString.java
    package JavaSamples;

    public class SplitString {
    public static void main(String args[]) {
    String input = "Java,Python,C++";
    String[] tokens = input.split(",");
    for(String res:tokens){
    System.out.println(res);
    }
    }
    }
Screenshots
  • STEP 1: The string is split by the specified delimiter (",").
  • Display Split Result