Re: Question about the split function



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

<needin4mation@xxxxxxxxx> wrote in message
news:1138304266.165749.54600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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.
>


.



Relevant Pages

  • RE: importing a fixed field length formatted text file into a dataset
    ... I think we have to parse the text file ... We can use the StreamReader to read the data from the file line by line and ... use the substring method of string to retrieve the fix length of text from ... the line of string. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: simple gsub question ` what?
    ... I need to parse a string: ... and I need to extract everything before the: (colon) into one variable ...
    (comp.lang.ruby)
  • Question about the split function
    ... public void ReadFromFile(string filename) ... StreamReader SR; ... string[] myStr; ... What I am trying to get is the stuff on the other side of the colon. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: simple gsub question ` what?
    ... I need to parse a string: ... and I need to extract everything before the: (colon) into one variable ...
    (comp.lang.ruby)
  • Re: String patterns. How to ?
    ... > I need to make sure a string is always formatted correctly. ... > If the user keys in: ... you require a colon then use Splitto parse the hours ...
    (microsoft.public.vb.general.discussion)

Loading