Hi,

I am trying to split a single line of data into three separate parts so that they can be displayed in different textboxes. The data looks like this:

item1_item2_item3

The code that I tried is:

Dim arrayLines() As String
Dim record As String

record = Subs.Text
arrayLines = record.Split("_")

TextBox1.Text = arrayLines(0)
TextBox2.Text = arrayLines(1)

I don't seem to have much luck, I don't even get an error message when I click on the button. Any suggestions..?

Dani AI

Generated

Nice fix by — the loop approach fills empty boxes reliably and, as reported, it worked for the original poster. Below are a few concise improvements and troubleshooting notes that make the split robust and avoid silent failures when the input is missing or when item three may itself contain underscores.

A safer pattern (limits splitting to three pieces, handles empty input and avoids index errors):

' leave the remainder as item3 even if it contains "_"
Dim record As String = Me.Subs.Text
If String.IsNullOrWhiteSpace(record) Then
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    Return
End If

Dim parts As String() = record.Split(New Char() {"_"c}, 3)

TextBox1.Text = parts(0)
TextBox2.Text = If(parts.Length > 1, parts(1).Trim(), String.Empty)
TextBox3.Text = If(parts.Length > 2, parts(2).Trim(), String.Empty)

Troubleshooting and best-practice notes:

  • A click that appears to do nothing is often the event handler not wired up. Confirm the method signature contains Handles ButtonName.Click or check the Designer's Click event.
  • Always guard against parts.Length before indexing to avoid runtime exceptions.
  • Use the "_ "c char literal (or New Char() {"_"c}) to keep Option Strict On safe.
  • If separators can vary or be multi-character, use the Split(String(), StringSplitOptions) overload or Regex.Split. Use StringSplitOptions.RemoveEmptyEntries when empty tokens should be ignored.

These tweaks keep the behavior predictable for inputs with missing pieces or extra underscores while preserving the simplicity of 's working solution.

Recommended Answers

All 2 Replies

hello !
please use this code this will solve your prob :)

Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = txtUsername.Text ' txtbox having txt you wana split.
        strArr = str.Split("_")
        For count = 0 To strArr.Length - 1
            If txt1.Text = "" Then
                txt1.Text = strArr(count).ToString

            Else
                If txt2.Text = "" Then
                    txt2.Text = strArr(count).ToString
                Else
                    If txt3.Text = "" Then
                        txt3.Text = strArr(count).ToString
                    End If
                End If
            End If

        Next

if your prob is solved then pleas mark this thread solved and also dont forget to add reputation .

Regards

As always, worked perfectly!! Thanks!

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.