Results 1 to 13 of 13

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

Hybrid View

  1. #1
    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.

  2. #2

    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