如何打印文本

时间:2016-10-12 05:58:42

标签: java string

public class usermain {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Map<String, String> map = new HashMap<String, String>();
    String test = "cat=\"dog\"; + abc=\"bcd\"; ";

        System.out.println(test);   


    // split on ':' and on '::'
    String[] parts = test.split("");

    for (int i = 0; i < parts.length; i += 2) {
        map.put(parts[i], parts[i + 1]);
    }

    for (String s : map.keySet()) {
        System.out.println(map);
    }

}

}

我想以数组显示:

[cat] = dog
[abc] = bcd

2 个答案:

答案 0 :(得分:0)

public class usermain {

public static void main(String[] args) {

    String test = "cat=\"dog\"; + abc=\"bcd\"; ";

        System.out.println(test);   


    String[] parts = test.split(" + ");

    for (int i = 0; i < parts.length; i += 1) {
        System.out.println("[" + parts.replace (';','').replace ("=","]="));
    }


}

}

答案 1 :(得分:0)

您可以使用适当格式的值进行拆分,然后使用entrySet()

打印键和值

参考 https://stackoverflow.com/a/3344768/3496666

String test =“cat = \”dog \“; + abc = \”bcd \“;”;

对于上面的字符串,首先将双引号,半冒号,空格和除“+”之外的其他字符替换为空字符

然后将带有'+'的字符串拆分为字符串数组,然后将'='拆分为字符串数组中的每个字符串。现在,您可以使用entrySet()

按预期显示输出