locked
Read string variable line by line? RRS feed

  • Question

  • Hello

    How can I read string variable line by line? and if it contains value before ";", put that value into another variable?

    thank you
    Saturday, February 16, 2008 1:26 PM

Answers

  •  

    "How can I read string variable line by line"

     

    If you have a string variable with crlf's in it you may do it like this:

     

      Dim Ar() as string =  split(stringB,environment.newline)

     

        For each s as string in AR

         ' s is the variable to process

         Textbox1.text += s

     

       Next  

     

     

     

    Saturday, February 16, 2008 3:53 PM
  • You can use regular expressions to accomplish this but we have to determine some delimiters.

     

    1. The following code splits the lines in a multiline textbox into an array.

    2. It then loops through the array to find all lines with a character length > 0 before ;

     

    Code Snippet

    Imports System.Text.RegularExpressions

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim s() As String = Regex.Split(TextBox1.Text, "\n")

    For Each s2 As String In s

    If Regex.IsMatch(s2.ToString, ".+(?=;)") Then

    MsgBox(s2.ToString)

    End If

    Next

    End Sub

    End Class

     

     

    Hope this helps,

    Adam
    Saturday, February 16, 2008 5:18 PM

All replies

  •  

    Do you mean read a file line by line?
    Saturday, February 16, 2008 2:42 PM
  • No, I mean variable with text inside. Because I'm using background worker control, and I think that is impossible to read file line by line using this control.

    Saturday, February 16, 2008 3:16 PM
  •  

    "How can I read string variable line by line"

     

    If you have a string variable with crlf's in it you may do it like this:

     

      Dim Ar() as string =  split(stringB,environment.newline)

     

        For each s as string in AR

         ' s is the variable to process

         Textbox1.text += s

     

       Next  

     

     

     

    Saturday, February 16, 2008 3:53 PM
  • "Because I'm using background worker control, and I think that is impossible to read file line by line using this control."

    Huh?  How does the BackgroundWorker affect file IO?

    Saturday, February 16, 2008 4:45 PM
  • You can use regular expressions to accomplish this but we have to determine some delimiters.

     

    1. The following code splits the lines in a multiline textbox into an array.

    2. It then loops through the array to find all lines with a character length > 0 before ;

     

    Code Snippet

    Imports System.Text.RegularExpressions

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim s() As String = Regex.Split(TextBox1.Text, "\n")

    For Each s2 As String In s

    If Regex.IsMatch(s2.ToString, ".+(?=;)") Then

    MsgBox(s2.ToString)

    End If

    Next

    End Sub

    End Class

     

     

    Hope this helps,

    Adam
    Saturday, February 16, 2008 5:18 PM
  • Thank you Smile
    Saturday, February 16, 2008 7:37 PM