如下字符串aaa.test.xxx.do想获取 test 和 xxx 应该是怎么截取的?

解决方案 »

  1.   

    public static void main(String[] args)
    {
    String s = "aaa.test.xxx.do";
    Pattern p = Pattern.compile("test");
    Pattern p1 = Pattern.compile("xxx"); Matcher m = p.matcher(s);
    Matcher m1 = p1.matcher(s);
    m.find();
    m1.find();
    String s1 = m.group();
    String s2 = m1.group();
    System.out.println(s1 + s2);
    }
    这么麻烦你还想用么
      

  2.   

    public class SplitString { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = "aaa.test.xxx.do";
    String[] strArrs = str.split("\\.");
    for(String strArr:strArrs){
    if(strArr.contains("test")){
    System.out.println("test");
    }
    if(strArr.contains("xxx")){
    System.out.println("xxx");
    }

    } }}
    -------------------------------------
    输出:
             test
             xxx