473,395 Members | 1,468 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,395 software developers and data experts.

ReadLine() from multiline string variable

just as the subject states, I need to find a way to read each line from a
multiline variable without having to write the string out to a file so I can
stream it in and use ReadLine(). Below is a snippet of code where I am trying
to read the string that has been passed into the function.
//source is the string of data to parse out
//nodes is the Xml Node Name in the mappingFile
("/Meters/Ion/Report/Field")
//ReadLine indicates whether to process line by line, or read as
continuous string
public List<List<Fields>GetNodeData(string source, string node,
Boolean ReadLine)
{
//Get the field mappings.
List<Fieldsrecord = GetFields(node);

List<List<Fields>records = new List<List<Fields>>();

using (StreamReader sr = new StreamReader(source))
{
//this is the string of data to process
string contents = sr.ReadLine();

while (sr.Peek() >= 0)
{...
Jul 20 '07 #1
4 10852
On Jul 19, 7:42 pm, sam01m <sam...@newsgroups.nospamwrote:
just as the subject states, I need to find a way to read each line from a
multiline variable without having to write the string out to a file so I can
stream it in and use ReadLine(). Below is a snippet of code where I am trying
to read the string that has been passed into the function.

//source is the string of data to parse out
//nodes is the Xml Node Name in the mappingFile
("/Meters/Ion/Report/Field")
//ReadLine indicates whether to process line by line, or read as
continuous string
public List<List<Fields>GetNodeData(string source, string node,
Boolean ReadLine)
{
//Get the field mappings.
List<Fieldsrecord = GetFields(node);

List<List<Fields>records = new List<List<Fields>>();

using (StreamReader sr = new StreamReader(source))
{
//this is the string of data to process
string contents = sr.ReadLine();

while (sr.Peek() >= 0)
{...
Try looking into the StringReader class.

Jul 20 '07 #2

Excellent, thank you (both) for the information. the only problem I'm
finding with ReadLine() though, in either implementation (StreamReader or
StringReader), is that I'm finding it strips out trailing whitespace. I have
preformatted data to be of a very specific length, whereby sometimes the last
two spaces have values, but more often than not, they are blank.

...........example line of data:

" 13:00SI 137.76 11.37 .00 4.01 "

...........when read through ReadLine() evaluates like this:

" 13:00SI 137.76 11.37 .00 4.01"

...........problem is, sometimes the input data looks like this:

" 13:00SI 137.76AD 11.37AD .00AD 4.01AD"

...........in any case, I need a fixed string length when I parse out each
ReadLine()

Any ideas?
Jul 20 '07 #3
sam01m wrote:
Excellent, thank you (both) for the information. the only problem I'm
finding with ReadLine() though, in either implementation (StreamReader or
StringReader), is that I'm finding it strips out trailing whitespace. I have
preformatted data to be of a very specific length, whereby sometimes the last
two spaces have values, but more often than not, they are blank.

..........example line of data:

" 13:00SI 137.76 11.37 .00 4.01 "

..........when read through ReadLine() evaluates like this:

" 13:00SI 137.76 11.37 .00 4.01"

..........problem is, sometimes the input data looks like this:

" 13:00SI 137.76AD 11.37AD .00AD 4.01AD"

..........in any case, I need a fixed string length when I parse out each
ReadLine()

Any ideas?
How have you determined that the ReadLine method removes the spaces?

I tried this code, and it shows that the spaces are not removed:

string input = " a\r\na \r\n a \r\na a\r\na";
using (StringReader reader = new StringReader(input)) {
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line.Length.ToString());
}
}

Output:
3
3
3
3
1

--
Göran Andersson
_____
http://www.guffa.com
Jul 20 '07 #4
Here is my code:

public void GetNodeData(string source, string node, Boolean ReadLine)
{
using (StringReader sr = new StringReader(source))
{
string contents;

if (ReadLine == true)
{
contents = sr.ReadLine();
}
else
{
contents=sr.ReadToEnd();
}

while (contents != null)
{

contents = sr.ReadLine();
}
}
}
Here is the input string taken directly from the "source" variable at
runtime. Notice that each line contains two spaces just before \r:

" 11:00 147.02 8.24 .00 1.80 \r\n
12:00 137.22 9.47 .00 3.29 \r\n
13:00SI 137.76 11.37 .00 4.01 \r"
Here is the result of "contents = sr.ReadLine();"

" 11:00 147.02 8.24 .00 1.80 "
" 12:00 137.22 9.47 .00 3.29 "
" 13:00SI 137.76 11.37 .00 4.01 "

Hmmm.... I stand corrected, my apologies!

Jul 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Dalberg | last post by:
I need to convert html into a multiline string which looks something like this: shtml = "<table border=""0"" cellspacing=""0"" cellpadding=""0"" width=""720"">" & _ "<tr><td width=""100%""...
12
by: Slonocode | last post by:
Hello I am trying to display a USA address properly in a multiline textbox. Unfortunately the address I must process is contained in a string variable and the format is not uniform. Examples: ...
3
by: ad | last post by:
I have a text file in the directory of my web application. How can I read this text file into a string vaiable?
11
by: Brian | last post by:
I have been working on a data reception system.. I am still finding my way around Javascript, though I am accomplishing much. I just fixed a flaw that was really hard to find. The symptoms are...
3
by: shawrie | last post by:
Hello everyone can anyone please help me? I basically want to set the length of a string variable to help spacing my simple report out. i tried dim test as string(14) but it didnt like that
8
by: =?gb2312?B?zrrW0Luq?= | last post by:
I want to create a multiline string that can works in ie and firefox, who have a better idea ?
4
by: Danny Shevitz | last post by:
Simple question here: I have a multiline string representing the body of a function. I have control over the string, so I can use either of the following: str = ''' print state return True...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
2
by: zcabeli | last post by:
Hi, i'd like to be able to search and replace a substring within a multiline string. for example, if i've got the following text. struct xyz { unsigned x; unsigned y; ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.