Hey i have a textbox that i want to read line by line and store in an ArrayList. I am currently trying to find the line break at the end of each line. Here is the code:

While i = 0
            h = ptext.IndexOf(vbCrLf)'here is where i try to find line break. i have tried everything from Chr(10) to vbLf
            If h < 0 Then
                h = ptext.Length
                i = 1
            End If
            a.Add(ptext.Substring(0, h))
            ln += 1
            If i = 0 Then ptext = ptext.Substring(h, ptext.Length)
            h = -7
        End While

Any suggestions?
thanks
m

Dani AI

Generated

Summary: ’s IndexOf/Substring loop ran into the usual pitfalls; ’s Split approach fixed the immediate need, highlighted checking for lone LF, and correctly called out the weak Variant declaration. The following expands on those hints with safer, more robust approaches and the exact loop bug to avoid.

Different sources can produce CRLF (\r\n), LF (\n) or CR (\r). To handle all of them reliably, split on any of those sequences rather than assuming one delimiter. In VB.NET a concise regex-based split works well:

Dim lines() As String = System.Text.RegularExpressions.Regex.Split(TextBox1.Text, "\r\n|\n|\r")

That yields each logical line; empty array entries correspond to blank lines and can be filtered if undesired.

When working in .NET, prefer the control’s built-in line support where available. The TextBox.Lines property already returns a string array (and preserves empty lines), so converting it to a typed collection is trivial and avoids manual parsing:

' VB.NET
Dim result As New List(Of String)(TextBox1.Lines)

The core bug in the original loop was not advancing past the delimiter when slicing the remaining text. After finding a line boundary the remainder must be taken starting at (index + delimiterLength); failing to do so can produce infinite loops or ArgumentOutOfRange exceptions. For very large text, stream with a StringReader.ReadLine loop to avoid large temporary arrays. Finally, prefer explicitly typed collections (List(Of String) or ArrayList with a clear element type) rather than loose Variants, and decide up front whether blank/whitespace-only lines should be kept or removed. These points build on and ’s suggestions and address the substring-advance and typing issues noted by .

Recommended Answers

All 7 Replies

Hi,

Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards
Veena

Hi,

Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards
Veena

A better way is as follows

If InStr(ptext,chr(10))> 0 Then
MsgBox " ptext contains chr10"
endif

regards
AV Manoharan

Hi Manoharan,

What if there are "Multiple Line Breaks" in textbox....?
Put in a loop..? Why do the excersise when it can be done in single statement.

Regards
Veena

Hi,

Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards
Veena

wow thanks, worked great!

The only problem is the MyArr declaration don't declare it like that (variant)

The only problem is the MyArr declaration don't declare it like that (variant)

yea instead of having debug print it, i added each one to an arraylist

Hi Manoharan,

What if there are "Multiple Line Breaks" in textbox....?
Put in a loop..? Why do the excersise when it can be done in single statement.

Regards
Veena

Veena, I think he is not using the textbox as a pipe or stream, so that they can contain any nubmebr of Carriage Returnes and Line Feeds. In a line of Text there can never be two such things.

Eventhough your programming code takes some overheads, it worked for him.

Thanks

AV Manoharan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.