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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting extra Chr(10) 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,558
US
I read some text data from my data base and write it to a text file with this code:

Code:
Using writer As StreamWriter = New StreamWriter("\\Some_Path\MyFile.txt")    
    While dtReader.Read = True
        writer.WriteLine(dtReader("Some_Field"))
    End While
End Using

And that works fine and gives me text in the txt file that looks fine to me:
[tt]
ABCD
EFGH
XYZX
....
[/tt]
In another program I want to read this txt file into an ArrayList

Code:
Private aryLNoGeom As New ArrayList
Private strFNoGeom() As String
...
Dim str As String = My.Computer.FileSystem.ReadAllText(MyFile.txt)
strFNoGeom = str.Split([red]Environment.NewLine[/red])

For z As Integer = 0 To UBound(strFNoGeom)[green]
    'aryLNoGeom.Add(Strings.Replace(strFNoGeom(z), Chr(10), ""))[/green]
    aryLNoGeom.Add(strFNoGeom(z))
Next

For z As Integer = 0 To aryLNoGeom.Count - 1
    Debug.Print("***" & aryLNoGeom(z) & "***")
Next

When I want to see what's in my ArrayList, I put *** before and after the value, and I keep getting:

[tt]***
ABCD***
***
EFGH***
***
XYZX***
....
[/tt]
I am getting Chr(10) before my value.

I have to strip this character (commented out line in green) and then I get what I want:
[tt]***ABCD***
***EFGH***
***XYZX***
....
[/tt]

Instead of [tt]Environment.NewLine[/tt] I tried vbNewLine and vbCrLf with the same problem.

What am I doing wrong?
How come I need to strip Chr(10) from my values to get what I need?
What do I need to change to NOT get extra Cht(10)?

Have fun.

---- Andy
 
Since the Split method doesn't take a string as the first parameter (it only takes character or string arrays), I suspect it's only using the first character of the vbCrLf string to perform the split and leaving the "vbLf" as part of each array element which is why you're getting the extra lines. What seems strange here is that it isn't giving you an error by using a simple string as the first parm. Here's some suggestions for fixing the problem:

Code:
strFNoGeom = str.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)

'or

strFNoGeom = str.Split({vbCrLf}, StringSplitOptions.None) 'Pass vbCrLf as an array of strings

'or

strFNoGeom = Split(str, vbCrLf) 'Use the Microsoft.VisualBasic.Strings function



 

Thank you Dave,
All of your suggestions work, and I even tried just
[tt]strFNoGeom = str.Split(vbCrLf.ToCharArray)[/tt]
and that worked, too.

Greetings from Ames :)

Have fun.

---- Andy
 
Hi Andy,
I thought I had tried the vbCrLf.ToCharArray without the RemoveEmptyEntries option and was getting an extra, empty split between the vbCr and the vbLf each line contained. Just make sure the extra entries aren't "hiding" due to them being control characters rather than printable characters.

And salutations from Marion/Cedar Rapids.
 
Thanks Dave,

I put back:
[tt]
strFNoGeom = str.Split(vbCrLf.ToCharArray, [blue] StringSplitOptions.RemoveEmptyEntries)[/blue][/tt]

just to be on the safe side....

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top