Results 1 to 7 of 7

Thread: Reading from a string line by line

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176

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

  2. #2
    pvb
    Guest
    I'm assuming since this was posted in the .NET section that you're using VB.NET.
    VB Code:
    1. Private Sub lineTest()
    2.     Dim myString As String = "line1" & vbCrLf & "line2" & vbCrLf & "line3"
    3.     Dim myArray As String()
    4.     Dim myLine As String
    5.     myArray = myString.Split(vbCrLf)
    6.     Debug.WriteLine(myString)
    7.     Debug.WriteLine("There are " & myArray.Length.ToString & " lines.")
    8.     Debug.WriteLine("The lines are:")
    9.     For Each myLine In myArray
    10.         Debug.WriteLine(myLine)
    11.     Next
    12. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    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.....

  4. #4
    pvb
    Guest
    made a couple of minor adjustments, maybe this will work better:
    VB Code:
    1. Private Sub lineTest()
    2.     Dim myString As String = "line1" & Chr(10) & "line2" & Chr(10) & "line3"
    3.     Dim myArray As String()
    4.     Dim myLine As String
    5.     myArray = myString.Split(Chr(10))
    6.     ListBox1.Items.Clear()
    7.     For Each myLine In myArray
    8.         Debug.WriteLine(myLine)
    9.         ListBox1.Items.Add(myLine)
    10.     Next
    11. End Sub

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    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?

  6. #6
    pvb
    Guest
    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:
    1. 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.)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    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
  •  



Click Here to Expand Forum to Full Width