![](/img/trans.png)
[英]string split with delimeters remain in strings result array, how? c#
[英]How to split string and keep delimeters in C#
我有一个流利的英文段落的字符串:
被卡住的无视在一场激烈的战斗下跋涉。 保险丝在电缆周围成功。 任何悔改的人都会批准适合的牛奶。 围巾在贸易围巾上方盛行。
我想将字符串拆分为空格' '
和所有标点符号等。例如: .,?;::
但将它们保留在字符串数组中。
我希望字符串在拆分后看起来像这样:
The
jammed
disregard
...
!
Any
...
到目前为止,我一直在尝试类似:
string s = "The jammed disregard treks below a fat combat. The fuse succeeds
around the cable! Any repent pole approves the suited milk. The
fence prevails above the trade scarf."
string[] splitStrings = Regex.Split(s, @"(?<=[.,?!;:])");
但我不确定如何在正则表达式中包含空格
编辑:为避免混淆:我正在尝试从英文段落中提取每个单词、空格和标点符号并将其放入字符串数组中(希望有帮助)。
我猜你想要这样的东西(根据你的输出)
var s = "The jammed disregard treks below a fat combat. The fuse succeeds around the cable! Any repent pole approves the suited milk. The fence prevails above the trade scarf.";
var split = Regex
.Replace(s, @"([ .,?!;:])", Environment.NewLine + "$1" + Environment.NewLine)
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in split)
Console.WriteLine(item);
Output
The
jammed
disregard
treks
below
a
fat
combat
.
The
fuse
succeeds
around
the
cable
!
Any
repent
pole
approves
the
suited
milk
.
The
fence
prevails
above
the
trade
scarf
.
这对你来说应该足够了
char[] delimiters = new char[]{ ' ', ':', ...other chars];
string[] splitter = myString.Split(delimiters).ToArray();
记下我放在数组中的空间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.