C#分割字符串并保留分隔符 [英] C# split string and keep separator

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

问题描述

嗨.我如何分割字符串并保持分隔符.我知道,它可以与Regex.Split一起使用,但是还有其他解决方案吗?
或者,如果需要分割括号和引号,我该如何使用Regex.Split.
因此,这是我需要在以下位置分割字符串的模式:
[]{}''"`(),;:!?
我该怎么做,或者如何使用正则表达式和该模式拆分字符串?

Hi. How i can split a string and keep separator. i know, that it is posible with Regex.Split, but are there other solutions?
or, how i can use Regex.Split, in case if i need to split parentheses and quotes.
so, here is the pattern i need to split string in:
[]{}''"`(),;:!?
how i can do this, or how i can split the string using regex and that pattern?
thanks.

推荐答案

最简单的解决方案是使用RegEx.这样,您可以选择将分隔符作为分组的一部分保留在分组之前,之后或什至是其自己的分组.

使用任何其他解决方案,您都必须编写代码来进行拆分,并支持您希望对分隔符进行的任何处理.
The easiest solution is to use a RegEx. That way you have the option of either keeping the seperator as part of the group before it, after it, or even it''s own group.

With any other solution, you''re going to have to write the code to do the splitting and support whatever disposition you want for the separator.


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

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