[英]Splitting a string by a space without removing the space?
你可以使用正則表達式,雖然它可能是一種矯枉過正:
StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"(?:\b\w+\b|\s)");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
matchResult = matchResult.NextMatch();
}
如果你只分開單詞邊界,你會得到一些非常接近你要求的東西。
string[] arr = Regex.Split("A quick brown fox.", "\\b");
arr [] = {“”,“A”,“”,“quick”,“”,“brown”,“”,“fox”,“。” }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.