String [] teststr = {"hello,123","world,456","asd,134","hello,567","world,678"};
在java中,我如何依次找到hello,并找到后面的123呢,如何依次找到world,并找到后面的数字呢,中间用,分割。说明,中间的其他字符不定,要找的字符串也不一定在第一个?最好是能贴出源代码,3Q。

解决方案 »

  1.   

    import java.util.HashMap;
    import java.util.Map;public class Test {
    public static void main(String[] args) {
    String[] teststr = {"hello,123","world,456","asd,134","hello,567","world,678"};
    for (int i = 0; i < teststr.length; i++) {
    String str = teststr[i];
    Map map = new HashMap();
    String[] newStr = str.split(",");
    map.put(newStr[0], newStr[1]);
    System.out.println(map.keySet());
    System.out.println(map.values());
    }
    }
    }
    这个是我写得。只是简单的拆分。希望能有所帮助
      

  2.   


    //打印每个元素,每行一个元素,前半部分和后半部分已经分开。
    String [] teststr = {"hello,123","world,456","asd,134","hello,567","world,678"};
    for(String subStr : teststr){
        String sub [] = subStr.slip(",");
        System.out.print(sub[0]);
        System.out.print('\t');
        System.out.println(sub[1]);
    }