473,406 Members | 2,343 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,406 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 2166
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: alexk | last post by:
I've a simple question. Why the following: words = "123#@$#$@^% wordB#@$".split('~`!@#$%^&*()_+-={},./') doesn't work? The length of the result vector is 1. I'm using ActivePython 2.4 Alex
11
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node>...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: morc | last post by:
hey, I have float values that look something like this when they are printed: 6.0E-4 7.0E-4 I don't want them to be like this I want them to be normalized with 4 decimal places.
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
4
by: Peter Proost | last post by:
Hi group, it's been a long time since the last time I've been here but I have a question. I'm working with timespan.parse for calculating a duration, I have to add strings which are in the...
6
by: zhengyan11 | last post by:
my job is to check the cookie and then decide if I show a image link on the page when the page is loaded. this is what I did: <html> <head> <TITLE>THIS IS TEST </TITLE>
1
by: John | last post by:
Hi I have written a Split function which in turn calls the standard string split function. Code is below; Function Split1(ByVal Expression As String, Optional ByVal Delimiter As String = " ",...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
2
by: gdarian216 | last post by:
I am writting a program that has three different files and is compiled by a Makefile. The goal is to take a file of text and split it up in different sections and stored in vectors. Then it outputs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.