简体   繁体   中英

Java Elegant splitting String by array

Have you any idea about elegant splitting input String by String Array?

For example:

  1. We have splitter array with String elements

    ["=",",=","<=",">=","<",">"]

  2. We have an expression

    "44+22<=x+y>=2x+4r>34"

The result should be:

["44+22","x+y", "2x+4r","34"]

Use the regex, (\\<\\=)|(\\>\\=)|(\\>) where | is used as or .

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String exp = "44+22<=x+y>=2x+4r>34";
        String[] arr = exp.split("(\\<\\=)|(\\>\\=)|(\\>)");
        System.out.println(Arrays.toString(arr));
    }
}

Output:

[44+22, x+y, 2x+4r, 34]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM