*Solved* How To Insert String Every Few Characters In Text
Hello!
I need to know how to insert a dash, for example, every 3 characters in string. (Then show it in a text box.) This is what I have right now but it doesn't do anything. What is wrong with this code?
Thanks!
~NinjaNic
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim YourText As String = TextBox1.Text
For i As Int32 = YourText.Length To 0 Step -3
YourText.Insert(i, "-")
Next
TextBox1.Text = YourText
End Sub
Re: How To Insert String Every Few Characters In Text
try this:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim YourText As String = TextBox1.Text
Dim hyphenCounter As Integer = 0
For i As Int32 = 3 To YourText.Length - 1 Step 3
YourText = YourText.Insert(i + hyphenCounter, "-")
hyphenCounter += 1
Next
TextBox1.Text = YourText
End Sub
End Class
Re: How To Insert String Every Few Characters In Text
[string].Insert is a function that returns a string
Re: How To Insert String Every Few Characters In Text
Oh, I understand now.
YourText.Insert(i + hyphenCount, "-")
becomes
YourText= YourText.Insert(i + hyphenCount, "-")
Thank you.