Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preserve Whitespace - newbie question

Status
Not open for further replies.

sbushway

Programmer
Jan 27, 2003
58
US
Bear with me- I'm sure this is a really easy question:

I'm reading a line from a fixed-width file that looks something like this:

JOHN DOE 123 MAIN ST NEW YORK

I want to preserve the whitespace when I read the file, but I don't know how to do that.

This is what I'm doing:
x = myStreamReader.ReadLine
label1.text = x.Substring(1,15)
label2.text = x.Substring(16,20)
label3.text = x.Substring(21,10)

label1 should be 'JOHN DOE ' (without the quotes of course)
label2 should be '123 MAIN ST '
label3 should be 'NEW YORK '

But what it's doing is setting label1 to 'JOHN DOE 123 MA'

Can anyone tell me how to preserve the whitespace?

Thanks!
Suzanne
 
Hi suzanne
it should not give if you use the Substring with Correct length & postionf of the field,as you said you have fixed lenght fields so you should know the length of the field and starting position
so if ur fields
label1.text=x.Substring(1,15) 'This field start at 0 and is of lenght of 15 chars
label2.text = x.Substring(16,20) 'This field start at 17 postion and is of lenght of 20 chars
label3.text = x.Substring(21,10) 'This field starts at 22 position and have 10 chars

Regards
Nouman


Nouman Zaheer
Software Engineer
MSR
 
Sorry - the substring is working fine. I realized that my problem is when I'm reading the line from the textfile.

The textfile contains the whitespace, but when I read that file, it strips it off.

Here's a snippet of my code:
Code:
Dim sr as StreamReader
Dim sw as StreamWriter
sr = File.OpenText("myfile.txt")
sw = File.CreateText("myoutput.txt")
dim x as string
dim stringtowrite as string
while sr.peek <> -1
  x = sr.readline
  label1.text = x
end while
sr.close()
sw.close()

Any ideas on how to prevent the StreamWriter from stripping the whitespace when it reads my file?
 
Hi
I don't know where in the snippet you are writing to the file? you are just reading it?
You said
&quot;The textfile contains the whitespace, but when I read that file, it strips it off.&quot;
i don't understand this either if you have white spaces then sr.readline will read the entire line with all spaces

Dim sr as StreamReader
Dim sw as StreamWriter
sr = File.OpenText(&quot;myfile.txt&quot;)
sw = File.CreateText(&quot;myoutput.txt&quot;)
dim x as string
dim stringtowrite as string
while sr.peek <> -1
x = sr.readline
sW.WriteLine(x)
end while
sr.close()
sw.close()

Regards
Nouman

Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top