How to split string by multiple same delimiters make split ignore them in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java,,,c++,,php,python";
        String[] arr = s.split(",");
 
        for (String str : arr) {
            System.out.println(str);
        }
        
        // Move the delimiters to end of string and split() will ignore them
        s = "java,c++,php,python,,,,,";
        arr = s.split(",");
 
        System.out.println("-------");
        for (String str : arr) {
            System.out.println(str);
        }
    }
}
 
 
 
/*
run:
 
java


c++

php
python
-------
java
c++
php
python

*/

 



answered Aug 1, 2020 by avibootz

Related questions

...