VB.NET – Trim all rows in a string

Dim my_string As String = vbTab & "row1  " & vbNewLine & "" & vbTab & "  row2  "

Dim rows As String() = my_string.Split(vbNewLine)
Dim text As StringBuilder = New StringBuilder

For Each row In rows

    Dim trimmed_row As String = row.Trim()
    text.Append(trimmed_row.ToString() & vbNewLine)

Next

Response.Write(text.ToString())

Or with Linq:

Dim trimmed_text = String.Join(vbLf, my_string.Split(vbLf).[Select](Function(s) s.Trim()))

Leave a Comment

Your email address will not be published. Required fields are marked *