Results 1 to 4 of 4

Thread: *Solved* How To Insert String Every Few Characters In Text

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    *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
    Last edited by NinjaNic; Apr 21st, 2015 at 09:20 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,238

    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

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,238

    Re: How To Insert String Every Few Characters In Text

    [string].Insert is a function that returns a string

  4. #4

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width