如何在java中获取字符串的一部分?

时间:2015-10-27 12:51:03

标签: java string

我想知道如何将长字符串的一部分带到字符串数组中。 例如turing: String str = "hi im john and my email is <john@hotmail.com> and my number is <0123456789>"String[] strarray = {"john@hotmail.com", "0123456789"}

1 个答案:

答案 0 :(得分:3)

这样的事情会起作用:

public static void main(String... args) throws Exception {
    String str = "hi im john and my email is <john@hotmail.com> and my number is <0123456789>";
    Pattern p = Pattern.compile("<(.*?)>");
    Matcher m = p.matcher(str);
    while(m.find()) {
        System.out.println(m.group(1));
    }
}

O / P:

john@hotmail.com
0123456789