java 使用单词分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6285568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting a String using Word Delimiters
提问by jch
i have a string as below
我有一个字符串如下
a > b and c < d or d > e and f > g
outcome must be:
结果必须是:
a > b
and
c < d
or
d > e
and
f > g
i want to split the string at occurrences of "and" , "or" and retrieve the delims as well along with the token.[i need them in order to evaluate the expression]
我想在出现“and”、“or”时拆分字符串并检索 delims 以及令牌。[我需要它们来评估表达式]
i tried using StringTokenizer as
我尝试使用 StringTokenizer 作为
new StringTokenizer(x, "\sand\s|\sor\s", true);
but i dont get desired outcome. i tried using scanner as
但我没有得到想要的结果。我尝试使用扫描仪作为
Scanner sc = new Scanner(x);
sc.useDelimiter("and | or");
this is able to split but doesnt return the delimiters.
这能够拆分但不返回分隔符。
please suggest.
请建议。
i have given a , b , c above but there cud be words instead of a,b , c with spaces. Updated example.
我在上面给出了 a , b , c 但是有单词而不是 a,b , c 和空格。更新示例。
回答by jeffb
This will split on "and" or "or" with any number of spaces surrounding the words.
这将拆分为“和”或“或”,单词周围有任意数量的空格。
String test = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";
String [] splitString = test.split("\s*[and|or]+\s*");
for(int i = 0; i < splitString.length ; i ++){
System.out.println(splitString[i]);
}
output
输出
2 < 3
3 > 2
4 < 6
7 < 8
回答by Stas Jaro
String delim = " ";
String[] splitstrings = yourString.split(delim);
for (int i = 0; i < splitstrings.length(); i++) {
splitstrings += delim;
}
回答by Romain Hippeau
回答by Voo
A StringTokenizer is the only java standard class that is capable of returning the used delimiter as well as far as I'm aware. Just copied the regex from the OT assuming that it'll do what he wants (which on second glance I extremely doubt from his textual description, but oh well - just insert the right one)
据我所知,StringTokenizer 是唯一能够返回使用过的分隔符的 Java 标准类。只是从 OT 复制了正则表达式,假设它会做他想做的事(乍一看,我非常怀疑他的文字描述,但哦,好吧 - 只需插入正确的)
String input = "a > b and c < d or d > e and f > g";
StringTokenizer tokenizer = new StringTokenizer(input, "\sand\s|\sor\s", true);
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
回答by takacsot
String str = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";
System.out.println( ImmutableList.copyOf( str.split( "(?=and|or)" ) ) );
Output:
输出:
[2 < 3 , and 3 > 2 , or 4 < 6 , and 7 < 8]