どのように文字列に文字に単語や語句を取得

溺死:

私は記号で囲まれた単語取得する必要がある« »文字列での:

String phrase = "«User» of the «application»";

String words[] = phrase.indexOf("«") + phrase.indexOf("»")

words[0] = "User";
words[1] = "application";

問題は、その解決策は、最初の単語だけ取得し、次のとおりです。「ユーザー」と私はすべての単語が額装必要があります...

どうやってやるの?

アービンド・クマールのAvinash:

次のように実行します。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        String phrase = "«User» of the «application»";
        Pattern p = Pattern.compile("«\\w+»");
        Matcher m = p.matcher(phrase);
        while (m.find()) {
            list.add(m.group().replaceAll("«", "").replaceAll("»", ""));
        }
        String words[] = list.toArray(new String[0]);
        System.out.println(Arrays.toString(words));
    }
}

出力:

[User, application]

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=14385&siteId=1