Click here to Skip to main content
14,694,465 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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();

}

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]
Posted
Updated 2-Jun-11 22:28pm
v3

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);
   }
   
Comments
shekarchee 3-Jun-11 3:07am
   
Awesome it works thanks to all
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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900