如何分割定界符保留在结果末尾的字符串? [英] How to split string that delimiters remain in the end of result?
本文介绍了如何分割定界符保留在结果末尾的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个定界符.例如{del1,del2,del3}.假设我有文本:Text1 del1 text2 del2 text3 del3
I have several delimiters. For example {del1, del2, del3 }. Suppose I have text : Text1 del1 text2 del2 text3 del3
我想用这种方式分割字符串:
I want to split string in such way:
- Text1 del1
- text2 del2
- text3 del3
当数组的每个元素都是texti deli时,我需要获取字符串数组.我该如何在C#中做到这一点?
I need to get array of strings, when every element of array is texti deli. How can I do this in C# ?
推荐答案
String.Split
允许使用多个拆分直径.我不知道这是否适合您的问题.
String.Split
allows multiple split-delimeters. I don't know if that fits your question though.
示例:
String text = "Test;Test1:Test2#Test3";
var split = text.Split(';', ':', '#');
//split contains an array of "Test", "Test1", "Test2", "Test3"
您可以使用正则表达式保留分号.
you can use a regex to keep the delimeters.
String text = "Test;Test1:Test2#Test3";
var split = Regex.Split(text, @"(?<=[;:#])");
// contains "Test;", "Test1:", "Test2#","Test3"
这篇关于如何分割定界符保留在结果末尾的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文