[英]Java - Splitting String based on multiple delimiters
I essentially want to split up a string based on the sentences, therefore (for the sake of what I'm doing), whenever there is a !
我基本上想要根据句子分割一个字符串,因此(为了我正在做的事情),每当有一个
!
, .
,
.
, ?
,
?
, :
, ;
,
:
, ;
. 。
How would I achieve this with multiple items to split the array with? 如何使用多个项目来分割数组?
Thanks! 谢谢!
String.split
使用正则表达式进行拆分,因此您可以简单地:
mystring.split("[!.?:;]");
Guava's Splitter
is a bit more predictable than String.split()
. Guava的
Splitter
比String.split()
更容易预测 。
Iterable<String> results = Splitter.on(CharMatcher.anyOf("!.?:;"))
.trimResults() // only if you need it
.omitEmptyStrings() // only if you need it
.split(string);
and then you can use Iterables.toArray
or Lists.newArrayList
to wrap the output results how you like. 然后你可以使用
Iterables.toArray
或Lists.newArrayList
来包装你喜欢的输出结果。
String.split的参数是一个正则表达式,因此您可以创建一个匹配任何这些字符的模式。
s.split("[.!:;?]");
您可以将String.split(String regex)方法与参数"[!.?:;]"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.