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 203 views
1 answer 218 views
2 answers 255 views
1 answer 216 views
1 answer 224 views
1 answer 209 views
2 answers 265 views
...