|
-
Mar 16th, 2002, 12:27 PM
#1
Thread Starter
Addicted Member
Reading from a string line by line
I want to read 1 line form a string that contains multiple lines. Put that line in an array, and read the next line and put into aray until there are no more lines in the string. I already asked this question on the normal VB board but since the 'line' statement isn't supported anymore they can't help me....
-
Mar 16th, 2002, 01:03 PM
#2
I'm assuming since this was posted in the .NET section that you're using VB.NET.
VB Code:
Private Sub lineTest()
Dim myString As String = "line1" & vbCrLf & "line2" & vbCrLf & "line3"
Dim myArray As String()
Dim myLine As String
myArray = myString.Split(vbCrLf)
Debug.WriteLine(myString)
Debug.WriteLine("There are " & myArray.Length.ToString & " lines.")
Debug.WriteLine("The lines are:")
For Each myLine In myArray
Debug.WriteLine(myLine)
Next
End Sub
-
Mar 16th, 2002, 01:13 PM
#3
Thread Starter
Addicted Member
it works but when i add it to a listbox there this stupid little square in front of the arr(1). that's the enter. How do i remove it? left/right doesn;t work anymore in VB.net.....
-
Mar 16th, 2002, 01:24 PM
#4
made a couple of minor adjustments, maybe this will work better:
VB Code:
Private Sub lineTest()
Dim myString As String = "line1" & Chr(10) & "line2" & Chr(10) & "line3"
Dim myArray As String()
Dim myLine As String
myArray = myString.Split(Chr(10))
ListBox1.Items.Clear()
For Each myLine In myArray
Debug.WriteLine(myLine)
ListBox1.Items.Add(myLine)
Next
End Sub
-
Mar 16th, 2002, 01:29 PM
#5
Thread Starter
Addicted Member
now that enter thing is on the end of every string..... i just need to remove the last item of the string all the times can u give me the .net code for that plz?
-
Mar 16th, 2002, 01:54 PM
#6
I'm not sure what needs to be removed... i guess if you need to remove _a_ character you can use the Replace method of the string object, eg:
VB Code:
myString = myString.Replace("something","someotherthing")
in my example using the Chr(10), when you use the Split method that character(the Chr(10)) is not included with any of the strings in the resulting array...i'm using beta 2 code still, maybe we're on different versions or something. (fyi, the Microsoft.VisualBasic namespace has all of the VB6 methods like Left, Right, Replace, etc.)
-
Mar 16th, 2002, 07:46 PM
#7
Thread Starter
Addicted Member
i fixed it by doing a split twice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|