Results 1 to 13 of 13

Thread: [RESOLVED] Adding space every two digits or letters

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Resolved [RESOLVED] Adding space every two digits or letters

    Can someone show me how can i add a space after every two digits, letters or both in a RichTextBox???

    In other words, instead of this:
    00000000000000000000
    00000000000000000000

    Should be this:
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00

    Thanks...

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Adding space every two digits or letters

    Just simply loop thru the characters of the string step 2 and build your new string by appending the current 2 chars and a space to a stringbuilder object.
    Some thing like this
    Code:
    Dim originalString As String = "your original string here"
    Dim sb As New System.Text.StringBuilder()
    Dim length As Integer = originalString.Length
    If length >= 2 Then
        sb.Append(originalString.Substring(0, 2) & " ")
        For i As Integer = 2 To length - 1 Step 2
              sb.Append(originalString.Substring(i, 2) & " ")
        Next
        If length Mod 2 <> 0 Then
              sb.Append(originalString(length - 1))
        End If
    Else
        sb.Append(originalString & " ")
    End If
    Dim modifiedString As String = sb.ToString
    Last edited by stanav; Mar 14th, 2008 at 10:29 PM.

  3. #3
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Adding space every two digits or letters

    vb Code:
    1. Function returnString(ByVal mystr As String, startpos As Integer) As String
    2.  
    3. if startpos > mystr.length - 2 Then
    4. Return mystr
    5. Else
    6.  
    7. Dim firstofst As string = Mid(mystr,1,startpos-1)
    8. Dim thetwo As String = Mid(mystr, startpos+ 1, 2)
    9. Dim newString As String = firstofst+thetwo+" "+Mid(mystr,startpos,mystr.length - startpos)
    10. startpos = startpos + 3
    11. returnString(newString, startpos)
    12.  
    13. End If
    14.  
    15. End Function

    That function should work, it just recursively get what you need, gets the string from the beginning up to the startposition, then gets the correct two characters, adds a space, then adds the rest of the string, and does this until there is less than 2 characters left in that string.

    If it doesn't work, please post back.

    Justin Fox
    Last edited by MonkOFox; Mar 14th, 2008 at 11:23 PM.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    I'm getting this error on this Line:
    returnString(newString)

    Error 36 Argument not specified for parameter 'startpos' of 'Public Function returnString(mystr As String, startpos As Integer) As String'. C:\Project1b.NET\frmMain.vb 1039 13 Project1

    I probably made a mistake, How should i call that function??

  5. #5
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Adding space every two digits or letters

    I fixed it in the previous post's code.

    I added the line " startpos = startpos + 3"

    and the function should be called like " returnString(newString,startpos)"

    Justin fox
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    ok, i really apreciate your help. I think i don't get it, should i need now to setup something else like telling where to look at?? i mean the name of the richtextbox? I'm getting Name not Declare for the newstring and the startpos on the command click event.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    I put the function just like you posted and on the command_Click event i'm calling the function, but i guess i still need to do something in the command_click event...sorry to be so noobbb

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    ok, the code seems to work but once it finish adding the spaces it goes back and remove it one by one. I notice that in the debug.


    Edited:
    The code seems to be mixing the strings, don't know why.

    Thanks for the help!
    Last edited by 2005; Mar 15th, 2008 at 12:23 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    I think i solve it with this:

    vb Code:
    1. Dim stringpos As Integer = 1
    2.         Dim i As Short
    3.         For i = 1 To Len(RTB2.Text)
    4.             Dim curstring As String = Mid(RTB2.Text, stringpos, 2)
    5.             RTB3.Text = RTB3.Text & curstring & " "
    6.             stringpos = stringpos + 2
    7.         Next
    8.         RTB2.Text = RTB3.Text
    9.         RTB3.Text = vbNullString

  10. #10
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Adding space every two digits or letters

    sorry about that mess. this works just fine.


    vb Code:
    1. Function returnString(ByVal mystr As String) As String
    2.         If mystr.Length < 2 Then
    3.             Return mystr
    4.         End If
    5.         Dim i As Integer = 0
    6.         Dim counter As Integer = 0
    7.         Dim newString As String = ""
    8.         While i < mystr.Length
    9.             newString = newString + mystr.Chars(i).ToString
    10.             i = i + 1
    11.             counter = counter + 1
    12.             If counter = 2 Then
    13.                 newString = newString + " "
    14.                 counter = 0
    15.             End If
    16.         End While
    17.         Return newString
    18.     End Function

    Justin Fox
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  11. #11
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Adding space every two digits or letters

    yea, you would just get the text in the richtexbox with

    Dim userInput As String = Me.myrichtexbox.text

    then

    me.resultLabel.Text = returnString(userInput)


    assuming that your richtextbox is named "myrichtexbox" and your label is named "resultLabel".


    Justin Fox
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    That worked very nice also.

    Thank you very much!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: Adding space every two digits or letters

    Quote Originally Posted by stanav
    Just simply loop thru the characters of the string step 2 and build your new string by appending the current 2 chars and a space to a stringbuilder object.
    Some thing like this
    Code:
    Dim originalString As String = "your original string here"
    Dim sb As New System.Text.StringBuilder()
    Dim length As Integer = originalString.Length
    If length >= 2 Then
        sb.Append(originalString.Substring(0, 2) & " ")
        For i As Integer = 2 To length - 1 Step 2
              sb.Append(originalString.Substring(i, 2) & " ")
        Next
        If length Mod 2 <> 0 Then
              sb.Append(originalString(length - 1))
        End If
    Else
        sb.Append(originalString & " ")
    End If
    Dim modifiedString As String = sb.ToString
    Stanav, thank you very very very much. That one was a lot faster code. Didn't understand it first but now i do and really run fast.

    Thanks to both for the help.

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