471,305 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question about the split function

Hi, I am trying to parse a colon delimited text file.

Assume these as examples:

Book:mybook
Video:myvideo
Name:myname
When I use this:

ReadFromFile("c:\\Inetpub\\wwwroot\\myfile.txt");
}
public void ReadFromFile(string filename)
{
StreamReader SR;
string S;
string[] myStr;
SR=File.OpenText(filename);
S=SR.ReadLine();
while(S!=null)
{
myStr = S.Split(new char[] { ':' });
Response.Write(myStr[0] + "<BR>");
S=SR.ReadLine();
}
SR.Close();
}

On my web page I get this:

Book
Video

What I am trying to get is the stuff on the other side of the colon.
How can I get either the data on the other side of the colon or just
the entire line?

Thank you for any help.

Jan 26 '06 #1
9 2084
It is working correctly you are just printing out the first value only.

Split returns an array of strings (myStr[0], myStr[1], etc.) that each
contain a value.

So in your example:
myStr[0] = Book
myStr[1] = mybook

myStr[0] = Video
myStr[1] = myvideo

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<ne***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi, I am trying to parse a colon delimited text file.

Assume these as examples:

Book:mybook
Video:myvideo
Name:myname
When I use this:

ReadFromFile("c:\\Inetpub\\wwwroot\\myfile.txt");
}
public void ReadFromFile(string filename)
{
StreamReader SR;
string S;
string[] myStr;
SR=File.OpenText(filename);
S=SR.ReadLine();
while(S!=null)
{
myStr = S.Split(new char[] { ':' });
Response.Write(myStr[0] + "<BR>");
S=SR.ReadLine();
}
SR.Close();
}

On my web page I get this:

Book
Video

What I am trying to get is the stuff on the other side of the colon.
How can I get either the data on the other side of the colon or just
the entire line?

Thank you for any help.

Jan 26 '06 #2
Let me fix your code...
First of all if you are trying to parse a file which is less than 10 MB u
may use File.OpenText otherwise u should use StreamReader

public void ReadFromFile(string pFilename)
{
StreamReader sr = null;
String sLine;
String[] stringData;
try
{
sr = File.OpenText(pFilename);
while (sr.Peek()>=0)
{
sLine= sr.ReadLine();
stringData= sLine.Split(':'); // Only available with 2.0
if (stringData.Length > 1)
Console.WriteLine("{0}\t{1}",stringData);
}
}
finally
{
if (sr != null)
sr.Close();
}
}

Also, u can access "mybook" using stringData[1]
--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET
Microsoft .NET & Security MVP

<ne***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi, I am trying to parse a colon delimited text file.

Assume these as examples:

Book:mybook
Video:myvideo
Name:myname
When I use this:

ReadFromFile("c:\\Inetpub\\wwwroot\\myfile.txt");
}
public void ReadFromFile(string filename)
{
StreamReader SR;
string S;
string[] myStr;
SR=File.OpenText(filename);
S=SR.ReadLine();
while(S!=null)
{
myStr = S.Split(new char[] { ':' });
Response.Write(myStr[0] + "<BR>");
S=SR.ReadLine();
}
SR.Close();
}

On my web page I get this:

Book
Video

What I am trying to get is the stuff on the other side of the colon.
How can I get either the data on the other side of the colon or just
the entire line?

Thank you for any help.

Jan 26 '06 #3
Thanks! Do you know if there is a way to start back over at the
beginning of the file? Sometimes there are error codes in a given text
file. I only want to parse the ones that have errors. So I thought
that if I searched once for "ERROR:" and if it had it I could go back
to the top of the file and start actually getting data. I thought this
might be better than getting data only to find out that I really don't
need to have captured from it in the first place. Doesn't sound like
much, but there could be a thousand text files to search through.

Jan 26 '06 #4
> Let me fix your code...

;)
stringData= sLine.Split(':'); // Only available with 2.0


What are you talking about? String.Split(char) has been with us for a long
time now. ;)

Anyway...I like mine better :)

using (StreamReader reader = File.OpenText("myfile.txt"))
{
while(reader.Peek() != -1)
{
string line = reader.ReadLine();

string[] parts = line.Split(':');

Console.WriteLine("{0} : {1}", parts[0], parts[1]);
}
}

Chris
Jan 26 '06 #5
If you only want to process lines that contain the string "ERROR:" before
the delimiter, you have no need to go through the file twice.

Simply test for the presence of "ERROR:" in myStr[0] (example):

sLine= sr.ReadLine();
stringData= sLine.Split(':');
if(stringData[0]=="ERROR")
{

// your cool processing code here
}

-- That's all.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"ne***********@gmail.com" wrote:
Thanks! Do you know if there is a way to start back over at the
beginning of the file? Sometimes there are error codes in a given text
file. I only want to parse the ones that have errors. So I thought
that if I searched once for "ERROR:" and if it had it I could go back
to the top of the file and start actually getting data. I thought this
might be better than getting data only to find out that I really don't
need to have captured from it in the first place. Doesn't sound like
much, but there could be a thousand text files to search through.

Jan 26 '06 #6
As you are creating a file stream you should be able to Seek to the
beginning.
A definition of SR like:

public void ReadFromFile(string filename) {
StreamReader SR;
string S;
string[] myStr;
FileStream fs = new FileStream(filename,FileMode.Open);
SR= new StreamReader(fs);
S=SR.ReadLine();
while(S!=null) {
myStr = S.Split(new char[] { ':' });
Response.Write(myStr[0] + "<BR>");
S=SR.ReadLine();
}
SR.Close();
}

You would get a FileStream to file called fs.
You could then use :
fs.Seek(0,SeekOrigin.Begin);

To jump back to the beginning of the file.

HTH

Ciaran
<ne***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks! Do you know if there is a way to start back over at the
beginning of the file? Sometimes there are error codes in a given text
file. I only want to parse the ones that have errors. So I thought
that if I searched once for "ERROR:" and if it had it I could go back
to the top of the file and start actually getting data. I thought this
might be better than getting data only to find out that I really don't
need to have captured from it in the first place. Doesn't sound like
much, but there could be a thousand text files to search through.

Jan 26 '06 #7
Ciaran <ci****@theodonnells.plus.com> wrote:
As you are creating a file stream you should be able to Seek to the
beginning.
<snip>
You would get a FileStream to file called fs.
You could then use :
fs.Seek(0,SeekOrigin.Begin);

To jump back to the beginning of the file.


Just to mention that a somewhat more readable (IMO) way of doing a Seek
is to use the Position property:

fs.Position = 0;

--
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
Jan 26 '06 #8
Hi Chris,
I really like your joke. The overload i had used with my code available
since 2.0. Original one was

"stringData = sLine.Split(new char[] { ':' },
StringSplitOptions.RemoveEmptyEntries);"
But then update my code for clarity. Thanks for your notice..

Anyway, using "using" keyword is same as using "try_finally" blocks. I used
to implement my codes using four languages (C#, VB.NET, J#, C++.NET) as
writing articles, writing posts.. And unfortunately using is not available
for J# and C++.NET yet :( Also it is available for VB.NET since 2.0... So I
prefer using "try_finally" blocks force of habit :)

--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET
Microsoft .NET & Security MVP

"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:44**************************@news.easynews.co m...
Let me fix your code...


;)
stringData= sLine.Split(':'); // Only available with 2.0


What are you talking about? String.Split(char) has been with us for a long
time now. ;)

Anyway...I like mine better :)

using (StreamReader reader = File.OpenText("myfile.txt"))
{
while(reader.Peek() != -1)
{
string line = reader.ReadLine();

string[] parts = line.Split(':');

Console.WriteLine("{0} : {1}", parts[0], parts[1]);
}
}

Chris

Jan 27 '06 #9
<"Yunus Emre ALPÖZEN [MVP]" <yemre>> wrote:

<snip>
Anyway, using "using" keyword is same as using "try_finally" blocks. I used
to implement my codes using four languages (C#, VB.NET, J#, C++.NET) as
writing articles, writing posts.. And unfortunately using is not available
for J# and C++.NET yet :( Also it is available for VB.NET since 2.0... SoI
prefer using "try_finally" blocks force of habit :)


Even if you're writing articles or posts though, I'd use idiomatic
language where possible - and the preferred idiom for disposing of
objects in C# *is* to use the using statement. Might as well use it in
languages where it's available...

(I would personally not use Peek, by the way - keep reading lines until
reader.ReadLine returns null.)

--
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
Jan 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by alexk | last post: by
6 posts views Thread by Senthil | last post: by
19 posts views Thread by morc | last post: by
13 posts views Thread by Eric_Dexter | last post: by
4 posts views Thread by Peter Proost | last post: by
6 posts views Thread by zhengyan11 | last post: by
1 post views Thread by John | last post: by
2 posts views Thread by gdarian216 | last post: by
reply views Thread by rosydwin | last post: by

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.