How to split a string with multiple separators in Java

1 Answer

0 votes
public class SplitExample {
    public static void main(String[] args) {
        String input = "abc,defg;hijk|lmnop-qrst_uvwxyz";
        // Match multiple separators: comma, semicolon, pipe, dash, underscore
        String[] result = input.split("[,;|\\-_]");

        for (String word : result) {
            System.out.println(word);
        }
    }
}



/*
run:

abc
defg
hijk
lmnop
qrst
uvwxyz

*/

 



answered Jul 21 by avibootz
...