473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Splitting String

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
1 1684
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
6
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.