|
-
Mar 14th, 2008, 10:16 PM
#1
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.
-
Mar 15th, 2008, 04:03 PM
#2
Thread Starter
Hyperactive Member
Re: Adding space every two digits or letters
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|