首页 > 解决方案 > 获取JAVA String[]中的特定元素

问题描述

我希望在 JAVA 的 String[] 中的字符串中获取名为Something的特定元素。

我的代码 =

String sentence = "AP=Something+example|AS=Explanation";
String[] word = sentence.split("\\|");
for (String w: word){
   System.out.println(w);
}

我当前的输出:

AP=Something+example
AS=Explanation

我的预期输出应该是:

Something // the other information I dont want to take. Is there a better and faster way and not too time consuming? 

先感谢您

标签: java

解决方案


我不明白你真正想要什么,但是:

        String sentence = "AP=Something+example|AS=Explanation";
        String[] recs = sentence.split("\\|");
        HashMap<String, String> h = new HashMap<String,String>();
        for (String r : recs) {
            String[] vals = r.split("=");
            h.put(vals[0], vals[1]);
        }
        System.out.println(h.get("AP").split("\\+")[0]);

这段代码太糟糕了,但看看常见的想法。

我的英语也太差了。


推荐阅读