C#分割字符串,但保持拆分字符/分离器 [英] C# split string but keep split chars / separators

查看:429
本文介绍了C#分割字符串,但保持拆分字符/分离器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分裂由三个不同的字符的字符串,但我想输出包括我分裂的人物。有没有简单的方法来做到这一点?

I'm splitting a string by three different characters but I want the output to include the characters I split by. Is there any easy way to do this?

推荐答案

如果拆分字符均; ,我想尝试:

If the split chars were ,, ., and ;, I'd try:

string[] parts = Regex.Split(originalString, @"(?<=[.,;])")

(小于?= PATTERN)为正查找背后 PATTERN 。它应该匹配在任何地方,preceding文本适合 PATTERN 所以应该有任何字符的每一次出现后,比赛(和分裂)。

(?<=PATTERN) is positive look-behind for PATTERN. It should match at any place where the preceding text fits PATTERN so there should be a match (and a split) after each occurrence of any of the characters.

这篇关于C#分割字符串,但保持拆分字符/分离器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆