簡體   English   中英

獲取JAVA String[]中的特定元素

[英]Get the specific element in JAVA String[]

我希望在 JAVA 的 String[] 中的字符串中獲取名為Some的特定元素。

我的代碼 =

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? 

先感謝您

如果您想在 plus 上拆分而不是在管道上拆分,則更改

String[] word = sentence.split("\\|");

String[] word = sentence.split("\\+");

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

        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]);

這段代碼太糟糕了,但看看共同的想法。

我的英語也太差了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM