473,396 Members | 2,037 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,396 software developers and data experts.

How do I split this String?

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
Jun 10 '06 #1
6 2146
I would replace \r with nothing, and then split on \n
Jun 10 '06 #2
After that a text.split("***".ToCharArray()) would do the farther
spliting of the users and the date!

Peter Morris [Droopy eyes software] wrote:
I would replace \r with nothing, and then split on \n


Jun 12 '06 #3
Jeoryos <va*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '06 #4
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 [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
Jeoryos <va*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 12 '06 #5
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

"Saurabh" <sa***********@hotmail.com> wrote in message
news:eD**************@TK2MSFTNGP04.phx.gbl...
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jeoryos <va*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 12 '06 #6
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.
Saurabh wrote:
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 [C# MVP]" <sk***@pobox.com <mailto:sk***@pobox.com>> wrote in
message news:MP************************@msnews.microsoft.c om...
> Jeoryos <va*****@gmail.com <mailto:va*****@gmail.com>> wrote:
> > 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.
>
> --
> Jon Skeet - <sk***@pobox.com <mailto:sk***@pobox.com>>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too

Jun 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.