By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Splitting String

P: n/a
I have a string that must be split out into a string array.

The segment delimiter is like this: {flag:xxx} and the "xxx" part is unknown
ahead of time (can be just about any character AND any number of characters
(at least one).
Here is a valid sample:
string sStartWithThis = "this goes to element one {flag:x} this goes to
element 2 {flag:pxedqr} this goes to element 3 {flag:pzf} this goes to
element 4.

How can I parse this string into an array of 4 elements? (none include the
{flag:...} delimiter).

Thanks!
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
string s = @"this goes to element one {flag:x} this goes to element 2
{flag:pxedqr} this goes to element 3 {flag:pzf} this goes to element 4.";
string[] sa = Split(s);
foreach(string ts in sa)
{
Console.WriteLine(ts);
}

public static string[] Split(string inString)
{
Regex r = new Regex("({flag:.+?})"); // Split on hyphens.
string[] sa = r.Split(inString);
for(int i=0; i< sa.Length; i++)
{
sa[i] = sa[i].Trim();
}
return sa;
}

If you don't want the "{flag:xxx}" delimiter in the result array, remove the
group "( )" parens in the Regex string.
So Regex("({flag:.+?})") would become Regex("{flag:.+?}") to give you this
output for above code:

this goes to element one
this goes to element 2
this goes to element 3
this goes to element 4.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Guadala Harry" <GM**@NoSpam.net> wrote in message
news:u0**************@TK2MSFTNGP14.phx.gbl...
I have a string that must be split out into a string array.

The segment delimiter is like this: {flag:xxx} and the "xxx" part is
unknown ahead of time (can be just about any character AND any number of
characters (at least one).
Here is a valid sample:
string sStartWithThis = "this goes to element one {flag:x} this goes to
element 2 {flag:pxedqr} this goes to element 3 {flag:pzf} this goes to
element 4.

How can I parse this string into an array of 4 elements? (none include the
{flag:...} delimiter).

Thanks!


Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.