分割字符串,但保留多个定界符 [英] splitstrings but keep the multiple delimiters

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

问题描述

大家好,

我改用了regex方法,很好地回答了以下问题.
字符串输入="MKWVTFISLLLLFSSAYSRGVFRRDTHKSEIAHRFKDLGEEHFKGLVLIAFSQYLQVK"

另一个问题:
如果我想跳过分隔符时说每3个而不是每次到达分隔符时该怎么办?

这样说
一个小姐"将显示为
MKMVTFISLLLLFSSAYSR
GVFR
R
DTHKSEIAHR等



如何使用使用多个分隔符的分割字符串方法,但保留分隔符?

例如:

Hi all,

I got a great answer the question below using the regex method instead.
string input = "MKWVTFISLLLLFSSAYSRGVFRRDTHKSEIAHRFKDLGEEHFKGLVLIAFSQYLQVK"

Another question:
What if I wanted to skip splitting at a delimiter say every 3rd instead of every time it reaches the delimiter?

so say
"one miss" would read
MKMVTFISLLLLFSSAYSR
GVFR
R
DTHKSEIAHR etc



How would I use the split string method using multiple delimeters but keep the delimeters?

Ex:

string input = "MKWVTFISLLLLFSSAYSRGVFRRDTHKSEIAHRFKDLGEEHFKGLVLIAFSQYLQVK";
string[] words = newString.Split(''R'',''K'');
for (int i = 0; i <; words.Length - 1; i++)
{
    sw.WriteLine(words[i]);
    char[] sumLine = words[i].ToCharArray();

}


我想读
MK
WVTFISLLLLFSSAYSR
GVFR
R
DTHK
SEIAHR等

基本上在K或R上分割字符串,但在对序列写行时保留K或R


[edit]已添加代码块-OriginalGriff [/edit]


I want it to read
MK
WVTFISLLLLFSSAYSR
GVFR
R
DTHK
SEIAHR etc etc

basically split the string on K or R but keep the K or R when I writeline the sequence


[edit]Code block added - OriginalGriff[/edit]

推荐答案

您最好的方法是使用Regex而不是Split:split不保留定界符:
Your best best is to use a Regex rather than Split: split does not keep the delimiter:
string input = "MKWVTFISLLLLFSSAYSRGVFRRDTHKSEIAHRFKDLGEEHFKGLVLIAFSQYLQVK";
Regex regex = new Regex("([^KR])*[KR]");
MatchCollection ms = regex.Matches(input);
foreach (Match m in ms)
   {
   Console.WriteLine(m.Value);
   }


正则表达式可能会帮助您-请尝试string[] words = Regex.Split(input, @"([RK])").
我想我把Regex弄错了-但我希望您能如愿以偿.
Regex will probsbly come to your rescue- try string[] words = Regex.Split(input, @"([RK])").
I think I''ve got the Regex wrong - but I hope you get the drift.


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

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