1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Inserting a hyphen after each 3 characters

Discussion in 'Software Development' started by TBH-LSVC, Nov 27, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. TBH-LSVC

    TBH-LSVC Thread Starter

    Joined:
    Aug 5, 2008
    Messages:
    91
    Hello,

    How can I insert a hyphen after each 3 characters in a textbox in VB.Net?
    For example, if in the text box there are: ABCDEFGHIJKLMNOP
    how can I insert a hyphen between each 3 letters automatically?
    The result would then be ABC-DEF-GHI-JKL-MNO-P.

    Further info:
    I have come to a way but it's not a practical one, i.e I used
    Insert(,"-")
    but then lines are filled with conditions slowing down the application. The portion of the code is:
    Code:
     Dim len As Integer
            len = Val.Text.Length()
            If len <= 3 Then GoTo r
            If 3 < len1 < 6 Then
                Val.Text = Val.Text.Insert(3, "-")
            ElseIf 6 < len1 < 9 Then
                Val.text=Val.text.Insert(3,"-")
                Val.text=Val.text.Insert(6,"-")
    
    and so on...
    
    Thanks in advance.
     
  2. OBP

    OBP Trusted Advisor

    Joined:
    Mar 8, 2005
    Messages:
    19,686
    What program are you using?
     
  3. TBH-LSVC

    TBH-LSVC Thread Starter

    Joined:
    Aug 5, 2008
    Messages:
    91
    Visual Studio 2008 and the language is VB.Net.
     
  4. TBH-LSVC

    TBH-LSVC Thread Starter

    Joined:
    Aug 5, 2008
    Messages:
    91
    Anybody knows please answer me because my whole application is stumbled at this point.
     
  5. bjf98

    bjf98

    Joined:
    Nov 8, 2009
    Messages:
    36
    Try something like this:
    Code:
    Dim mytext = Val.text
    Dim hyphenated
    Dim count = 1
    Dim x = Round(Val.Text.Length / 3, 0)
    For 0 To x
    hyphenated += Mid(mytext, count, 3) & "-"
    count +=3
    Next
    Val.text = hyphenated
    
     
  6. TBH-LSVC

    TBH-LSVC Thread Starter

    Joined:
    Aug 5, 2008
    Messages:
    91
    Thanks, it is not of correct syntax; nevertheless, the problem was solved.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/880775

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice