472,119 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Going through user input (textbox) line by line

Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.
Nov 17 '05 #1
6 6513
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your function.
This way, if you want to pass the string contents to the function, you can
just pass the string through a StringReader instance and the function will
accept it (it acts like a stream, allowing you to read character after
character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.

Nov 17 '05 #2
Thanks... do you have an example? really stuck here.

Thanks
Maz
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your
function. This way, if you want to pass the string contents to the
function, you can just pass the string through a StringReader instance and
the function will accept it (it acts like a stream, allowing you to read
character after character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change my
program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.


Nov 17 '05 #3
Maziar,

You could just do something like this:

public void Parse(TextReader reader)
{
// Read line by line here.
string line;

// Read.
while ((line = reader.ReadLine()) != null)
{
// Parse the line.

}
}

That's really all there is to it. You probably have something very
similar in your code that does this right now, you just have to make it work
with a TextReader.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Qv********************@rogers.com...
Thanks... do you have an example? really stuck here.

Thanks
Maz
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Maziar,

You should refactor the logic part of your application so that your
function takes a TextReader. Once you have that, you can take your
FileStream and then wrap it in a StreamReader, and pass it to your
function. This way, if you want to pass the string contents to the
function, you can just pass the string through a StringReader instance
and the function will accept it (it acts like a stream, allowing you to
read character after character from the string you pass in).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Re********************@rogers.com...
Hi,

I have a little application that reads a text file line-by-line and
processes each line depending on the CVS values. Now I want to change
my program to capture this from a textbox. How do I parse a string
line-by-line? Just like a file?

Thanks
Maz.



Nov 17 '05 #4
In article <Qv********************@rogers.com>,
Maziar Aflatoun <ma***@rogers.com> wrote:

: Thanks... do you have an example? really stuck here.

Here's a simple example:

public static void WriteFile(string path, string contents)
{
using (StreamWriter w = new StreamWriter(path))
w.Write(contents);
}

public static void LineByLine(TextReader reader)
{
Console.WriteLine("input type = " + reader.GetType());

int n = 1;
string line;
while ((line = reader.ReadLine()) != null)
Console.WriteLine(n++ + ". [" + line + "]");
}

static void Main(string[] args)
{
string text =
"The quick\n" +
"brown fox jumped over\n" +
"the lazy dog.";

string path = "foo.txt";
WriteFile(path, text);
using (StreamReader streamr = new StreamReader(path))
LineByLine(streamr);

TextBox textbox = new TextBox();
textbox.Text = text;
using (StringReader stringr = new StringReader(text))
LineByLine(stringr);
}

Hope this helps,
Greg
Nov 17 '05 #5
In article <11*************@corp.supernews.com>,
Greg Bacon <gb****@hiwaay.net> wrote:

: using (StringReader stringr = new StringReader(text))

Oops, that should be

using (StringReader stringr = new StringReader(textbox.Text))

but the point is the same.

Greg
Nov 17 '05 #6
Thank you so much :)

"Greg Bacon" <gb****@hiwaay.net> wrote in message
news:11*************@corp.supernews.com...
In article <Qv********************@rogers.com>,
Maziar Aflatoun <ma***@rogers.com> wrote:

: Thanks... do you have an example? really stuck here.

Here's a simple example:

public static void WriteFile(string path, string contents)
{
using (StreamWriter w = new StreamWriter(path))
w.Write(contents);
}

public static void LineByLine(TextReader reader)
{
Console.WriteLine("input type = " + reader.GetType());

int n = 1;
string line;
while ((line = reader.ReadLine()) != null)
Console.WriteLine(n++ + ". [" + line + "]");
}

static void Main(string[] args)
{
string text =
"The quick\n" +
"brown fox jumped over\n" +
"the lazy dog.";

string path = "foo.txt";
WriteFile(path, text);
using (StreamReader streamr = new StreamReader(path))
LineByLine(streamr);

TextBox textbox = new TextBox();
textbox.Text = text;
using (StringReader stringr = new StringReader(text))
LineByLine(stringr);
}

Hope this helps,
Greg

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tim::.. | last post: by
2 posts views Thread by Tim::.. | last post: by
reply views Thread by Tim::.. | last post: by
3 posts views Thread by Tim::.. | last post: by
15 posts views Thread by Doogie | last post: by
reply views Thread by leo001 | 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.