简体   繁体   English

具有多个分隔符的Split()(不起作用)

[英]Split() with multiple delimiters(Not working)

So I have a file to which I have written into an array. 因此,我有一个已写入数组的文件。 I wanted to parse the string by splitting it into smaller strings. 我想通过将字符串拆分为较小的字符串来解析该字符串。 I have used split("\\s+") to separate a string from another string by any space. 我已经使用split(“ \\ s +”)通过任何空格将一个字符串与另一个字符串分开。 However I wanted to also include (";,.\\n:()") as delimiters. 但是我也想包括(";,.\\n:()")作为分隔符。 Can someone help me? 有人能帮我吗? I have tried split(".,;:\\n()\\\\s+") but this produces a way wrong result. 我试过split(".,;:\\n()\\\\s+")但这会导致错误的结果。

Do like this, 这样吧

str.split("[.,;:\\n()]|\\s+");

character class should take each token as separate delimiters. 字符类应将每个标记用作单独的分隔符。 Above should do splitting according to the characters present inside the character present inside the character class or it would do splits on one or more space characters. 上面的代码应该根据字符类中存在的字符中的字符进行拆分,否则将对一个或多个空格字符进行拆分。

or 要么

Since \\\\s also matches \\\\n , I just combined the both.. 由于\\\\s也匹配\\\\n ,因此我将两者组合在一起。

str.split("[.,;:()\\s]+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM