我有一个这样的字符串
name=jack
age=30
edu=collage
各个属性剑使用换行符分割,我想获取每个属性的值,如name为jack,用什么方法才是最科学的呢

解决方案 »

  1.   

    我过去式将该字符串使用回车先进行分割,然后判断数组每个值中是否含有关键值如name=,然后根据返回的索引值再进行截川,不过感觉这样的方法效率应该比较低,不知道有没有其他方法
      

  2.   

    字符串解析后放到map里    public Map<String, String> getProperties(String str) throws Exception {
            HashMap<String, String> properties = new HashMap();        String cStr = str.replace("¥r", "¥n").replace("¥n¥n", "¥n");
            Scanner sc = new Scanner(cStr);
            sc.useDelimiter("¥n");
            while (sc.hasNext()) {
                String prop = sc.next();
                String[] splitValue = prop.split("=");
                if (splitValue.length == 2) {
                    properties.put(splitValue[0], splitValue[1]);
                } else {
                    throw new Exception("String:" + str + "format Error!");
                }
            }        return properties;
        }