How do I split this String?

S

Saurabh

Hi everyone,

I am looking for some expert advise to get me out of trouble. I am looking for a solution in C# which will allow me to split the below string in the format provided. The String.Split() allows only a single char as a delimiter.So I cannot use that as well.

Original String

"*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm \r\nThis is the first line of text. There will be many more beneath this on so look out.\r\n\r\n*** JON *** December 4, 1995 at 8:49am \r\nPoint your browser to http://support.microsoft.com and visit our Support Services web site for technical assistance, news, and manuals.\r\n\r\n*** JON *** November 29, 1995 at 7:39pm \r\nWe offer support through a network of partners who provide product training, implementation and consulting support.\r\n\r\n\0ls.\r\n\r\n\0"

Format Required

string1 = "MASTER (Supervisor)
string2 = "September 23, 1997 at 3:22pm"
string3 = "This is the first line of text. There will be many more beneath this on so look out."
string4 = "JON"
string5 = "December 4, 1995 at 8:49am
string6 = "Point your browser to http://support.microsoft.com and visit our Support Services web site for technical assistance, news, and manuals."
string7 = "JON"
string8 = "November 29, 1995 at 7:39pm"
string9 = "We offer support through a network of partners who provide product training, implementation and consulting support."

So you see, I need to sort the entire text. Any help in this regard would be highly appreciated.

Have a great day,
Saurabh
 
J

Jeoryos

After that a text.split("***".ToCharArray()) would do the farther
spliting of the users and the date!
 
J

Jon Skeet [C# MVP]

Jeoryos said:
After that a text.split("***".ToCharArray()) would do the farther
spliting of the users and the date!

No it wouldn't. That would split by the * character, not *** as a
string. In other words, it would split Foo*Bar into Foo and Bar, which
isn't what's wanted as far as I can see.
 
S

Saurabh

Thanks for your suggestions. This was close but not the final output what I am looking for. Here is what I got after splitting for \r and \n

[0] "*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm "
[1] ""
[2] "This is the first line of text. There will be many more beneath this on so look out."
[3] ""
[4] ""
[5] ""
[6] "*** JON *** December 4, 1995 at 8:49am "
[7] ""
[8] "Point your browser to http://support.microsoft.com and visit our Support Services web site for technical assistance, news, and manuals."

Any ideas on how to split for users and date. Also how do I know the line or the index where i have a text. The idea is to insert the record only if there some note text is available?

Thanks for all the help so far.

~Saurabh~
 
C

Claes Bergefall

There is a new String.Split in 2.0 that takes an array of strings as
separators. Use that and split on "***":
text.Split(new String[]{"***"}, Int32.Max, None);

You should even be able to split your original string in one go by using
this:
text.Split(new String[] { "\r\n", "***" }, Int32.Max, None);

/claes

Thanks for your suggestions. This was close but not the final output what I
am looking for. Here is what I got after splitting for \r and \n

[0] "*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm "
[1] ""
[2] "This is the first line of text. There will be many more beneath this
on so look out."
[3] ""
[4] ""
[5] ""
[6] "*** JON *** December 4, 1995 at 8:49am "
[7] ""
[8] "Point your browser to http://support.microsoft.com and visit our
Support Services web site for technical assistance, news, and manuals."

Any ideas on how to split for users and date. Also how do I know the line or
the index where i have a text. The idea is to insert the record only if
there some note text is available?

Thanks for all the help so far.

~Saurabh~
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

If the basic format of the string doesn't change, you could use a
regular expression to parse it.

The pattern would be something like:

"*** (.*?) *** (.*?) \r\n(.*?)\r\n\r\n*** (.*?) *** (.*?)
\r\n(.*?)\r\n\r\n*** (.*?) *** (.*?) \r\n(.*?)\r\n\r\n\0ls.\r\n\r\n\0"

That would give you the strings you requestsed with the input you
showed. You may need to make the pattern more flexible if the format of
the input can vary.

Thanks for your suggestions. This was close but not the final output
what I am looking for. Here is what I got after splitting for \r and \n

[0] "*** MASTER (Supervisor) *** September 23, 1997 at 3:22pm "
[1] ""
[2] "This is the first line of text. There will be many more beneath
this on so look out."
[3] ""
[4] ""
[5] ""
[6] "*** JON *** December 4, 1995 at 8:49am "
[7] ""
[8] "Point your browser to http://support.microsoft.com and visit our
Support Services web site for technical assistance, news, and manuals."

Any ideas on how to split for users and date. Also how do I know the
line or the index where i have a text. The idea is to insert the record
only if there some note text is available?

Thanks for all the help so far.

~Saurabh~


Jon Skeet said:
No it wouldn't. That would split by the * character, not *** as a
string. In other words, it would split Foo*Bar into Foo and Bar, which
isn't what's wanted as far as I can see.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top