Question about the split function

N

needin4mation

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.
 
C

C.C. \(aka Me\)

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
 
Y

Yunus Emre ALPÖZEN [MVP]

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
 
N

needin4mation

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.
 
C

chris martin

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
 
G

Guest

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
 
C

Ciaran

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
 
J

Jon Skeet [C# MVP]

Ciaran said:
As you are creating a file stream you should be able to Seek to the
beginning.

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;
 
Y

Yunus Emre ALPÖZEN [MVP]

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 said:
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
 
J

Jon Skeet [C# MVP]

<"Yunus Emre ALPÖZEN [MVP]" <yemre>> wrote:

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.)
 

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