C#:拆分带分隔符的字符串,但保留定界符的结果 [英] C#: split a string with delimiters but keep the delimiters in the result

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

问题描述

我想在结果拆分带分隔符的字符串但保留分隔符

I would like to split a string with delimiters but keep the delimiters in the result

我会怎么做这在C#?

在此先感谢!

推荐答案

如果您想要的分隔符是自己的分裂,你可以使用的 Regex.Split 例如:

If you want the delimiter to be its "own split", you can use Regex.Split e.g.:

string input = "plum-pear";
string pattern = "(-)";

string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
foreach (string match in substrings)
{
   Console.WriteLine("'{0}'", match);
}
// The method writes the following to the console:
//    'plum'
//    '-'
//    'pear'

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

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