I want to make a function of it. Do you think it is possible? that is, not to have to write this code hundreds of times.
Dim pieces() As String = Textbox1.Text.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Dim str As String = ""
For Each piece As String In pieces
str &= String.Join(",", piece.ToCharArray().ToList()) & ControlChars.CrLf
Next
Textbox2.Text = str
CodePudding user response:
To create a Function
you need a return type, in this case String
and a Return
statement.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str = AddCommas(TextBox1.Text)
TextBox2.Text = str
End Sub
Private Function AddCommas(inputString As String) As String
Dim pieces() As String = inputString.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Dim str As String = ""
For Each piece As String In pieces
str &= String.Join(",", piece.ToCharArray().ToList()) & ControlChars.CrLf
Next
Return str
End Function