473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Field s>GetNodeData(s tring source, string node,
Boolean ReadLine)
{
//Get the field mappings.
List<Fieldsreco rd = GetFields(node) ;

List<List<Field s>records = new List<List<Field s>>();

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

while (sr.Peek() >= 0)
{...
Jul 20 '07 #1
4 10868
On Jul 19, 7:42 pm, sam01m <sam...@newsgro ups.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<Field s>GetNodeData(s tring source, string node,
Boolean ReadLine)
{
//Get the field mappings.
List<Fieldsreco rd = GetFields(node) ;

List<List<Field s>records = new List<List<Field s>>();

using (StreamReader sr = new StreamReader(so urce))
{
//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.

...........exam ple 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"

...........prob lem 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.

..........examp le 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"

..........probl em 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(in put)) {
while ((line = reader.ReadLine ()) != null) {
Console.WriteLi ne(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(str ing source, string node, Boolean ReadLine)
{
using (StringReader sr = new StringReader(so urce))
{
string contents;

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

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
1869
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%"" valign=""top""><br><!-- Main Table -->" & _ .... Is there a tool that can do this?
12
1575
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: John Smith, 12345 Main Street, Anytown, NY 98776 John Smith, 12345 Main Street, Anytown, NY, 98876 John Smith, c/o Jane Smith, 12345 Main Street, Anytown, NY 98876 John Smith, c/o Jane Smith, 12345 Main Street, Anytown, NY, 98876
3
2279
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
5085
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 this: I get a multiline string returned to Javascript from a Proxy+Google Maps API GDownloadUrl() The data, when added to a DOM table looked fine, about 20 lines in CSV format
3
1882
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
8027
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
2129
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
2025
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 the variable query? public class MyClass {
2
2836
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; unsigned z; }
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.