文字列をトークンに分割します。文字列を複数の区切り文字で分割します。
私は別のスタックオーバーフローの質問 - Equivalent to StringTokenizer with multiple characters delimitersをリッピングしましたが、これは文字列メソッド(.equals()、.startsWith()などでのみ実行できるかどうかを知りたいです。私は、RegEx、StringTokenizerクラス、Patterns、Matchers、またはString
以外のものは使用しません。
例えば、これは私が法に
String[] delimiters = {" ", "==", "=", "+", "+=", "++", "-", "-=", "--", "/", "/=", "*", "*=", "(", ")", ";", "/**", "*/", "\t", "\n"};
String splitString[] = tokenizer(contents, delimiters);
を呼び出し、これは私が他の質問(私はこれを行うにはしたくない)のリッピングコードでする方法です。
private String[] tokenizer(String string, String[] delimiters) {
// First, create a regular expression that matches the union of the
// delimiters
// Be aware that, in case of delimiters containing others (example &&
// and &),
// the longer may be before the shorter (&& should be before &) or the
// regexpr
// parser will recognize && as two &.
Arrays.sort(delimiters, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return -o1.compareTo(o2);
}
});
// Build a string that will contain the regular expression
StringBuilder regexpr = new StringBuilder();
regexpr.append('(');
for (String delim : delimiters) { // For each delimiter
if (regexpr.length() != 1)
regexpr.append('|'); // Add union separator if needed
for (int i = 0; i < delim.length(); i++) {
// Add an escape character if the character is a regexp reserved
// char
regexpr.append('\\');
regexpr.append(delim.charAt(i));
}
}
regexpr.append(')'); // Close the union
Pattern p = Pattern.compile(regexpr.toString());
// Now, search for the tokens
List<String> res = new ArrayList<String>();
Matcher m = p.matcher(string);
int pos = 0;
while (m.find()) { // While there's a delimiter in the string
if (pos != m.start()) {
// If there's something between the current and the previous
// delimiter
// Add it to the tokens list
res.add(string.substring(pos, m.start()));
}
res.add(m.group()); // add the delimiter
pos = m.end(); // Remember end of delimiter
}
if (pos != string.length()) {
// If it remains some characters in the string after last delimiter
// Add this to the token list
res.add(string.substring(pos));
}
// Return the result
return res.toArray(new String[res.size()]);
}
public static String[] clean(final String[] v) {
List<String> list = new ArrayList<String>(Arrays.asList(v));
list.removeAll(Collections.singleton(" "));
return list.toArray(new String[list.size()]);
}
編集:私はONLYのcharAt、等しく、equalsIgnoreCase、のindexOf、長さの文字列メソッドを使用すると、サブ
うわー、それは複雑です。私の答えを見てください。 https://en.wikipedia.org/wiki/KISS_principle – NickJ
@NickJ Haha、私はもっと楽にしたいと思う。しかし、これは私がしなければならないプロジェクトのためのものです... –
スレートハンマーで飛んで –