How to split a string with multiple separators into a collection in Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import java.util.stream.Collectors;
 
public class MyClass {
    public static void main(String args[]) {
        String str = "= java, . c, = c++, | rust, ? python";
         
        Collection<String> co = Arrays.stream(str.split("[=,|.?]"))
                                .map(String::trim)
                                .filter(next -> !next.isEmpty())
                                .collect(Collectors.toList());


        System.out.println(co);
    }
}
 
 
 
 
 
/*
run:
 
[java, c, c++, rust, python]
 
*/

 



answered Mar 13, 2023 by avibootz
edited Mar 13, 2023 by avibootz

Related questions

1 answer 91 views
1 answer 130 views
2 answers 108 views
1 answer 102 views
1 answer 97 views
1 answer 95 views
2 answers 123 views
...