.Net拆分,保留拆分字符 [英] .Net Split with split characters retained

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

问题描述

可能重复:
C#拆分字符串,但保留拆分字符/分隔符

Possible Duplicate:
C# split string but keep split chars / separators

是否有一种简单的方法来执行.Net字符串split()函数,该函数会将原始的拆分字符保留在结果中?

Is there a simple way to do a .Net string split() function that will leave the original split characters in the results?

如此:

"some text {that|or} another".Split('{','|','}'); 

将导致一个数组:

[0] = "some text "
[1] = "{"
[2] = "that"
[3] = "|"
...

最好不使用正则表达式.

Preferably without a regex.

推荐答案

签出

check out this post

第一个关于RegEx解决方案的答案,第二个关于非正则表达式解决方案的答案...

the first answer with a RegEx solution, the second for a non-regex solution...

概念上...

string source = "123xx456yy789";
foreach (string delimiter in delimiters)
    source = source.Replace(delimiter, ";" + delimiter + ";");
string[] parts = source.Split(';');

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

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