Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
added 2 characters in body
Source Link
Dmytro Shvechikov

To summarize: there are at least five ways to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).

HereHere is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)

To summarize: there are at least five ways to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)

To summarize: there are at least five ways to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)

Active reading.
Source Link
Peter Mortensen

To summarize: there are at least 5five ways how to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you dependsdepending on what you need, e.g. return type (array, list, or iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)

To summarize: there are at least 5 ways how to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depends on what you need, e.g. return type (array, list, iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark etc.)

To summarize: there are at least five ways to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)

Source Link
Dmytro Shvechikov

To summarize: there are at least 5 ways how to split a string in Java:

  1. String.split():

     String[] parts ="10,20".split(",");
    
  2. Pattern.compile(regexp).splitAsStream(input):

     List<String> strings = Pattern.compile("\\|")
           .splitAsStream("010|020202")
           .collect(Collectors.toList());
    
  3. StringTokenizer (legacy class):

     StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", ".");
     while(strings.hasMoreTokens()){
         String substring = strings.nextToken();
         System.out.println(substring);
     }
    
  4. Google Guava Splitter:

     Iterable<String> result = Splitter.on(",").split("1,2,3,4");
    
  5. Apache Commons StringUtils:

     String[] strings = StringUtils.split("1,2,3,4", ",");
    

So you can choose the best option for you depends on what you need, e.g. return type (array, list, iterable).

Here is a big overview of these methods and the most common examples (how to split by dot, slash, question mark etc.)

lang-java