Going through user input (textbox) line by line

  • Thread starter Maziar Aflatoun
  • Start date
M

Maziar Aflatoun

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

Nicholas Paldino [.NET/C# MVP]

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

Maziar Aflatoun

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

Thanks
Maz


Nicholas Paldino said:
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]
- (e-mail address removed)

Maziar Aflatoun said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

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

Thanks
Maz


Nicholas Paldino said:
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]
- (e-mail address removed)

Maziar Aflatoun said:
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.
 
G

Greg Bacon

: 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
 
G

Greg Bacon

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

Oops, that should be

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

but the point is the same.

Greg
 

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