![](/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.