|
-
Mar 14th, 2008, 10:00 PM
#1
Thread Starter
Hyperactive Member
[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...
-
Mar 14th, 2008, 10:16 PM
#2
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 14th, 2008, 10:23 PM
#3
Frenzied Member
Re: Adding space every two digits or letters
vb Code:
Function returnString(ByVal mystr As String, startpos As Integer) As String
if startpos > mystr.length - 2 Then
Return mystr
Else
Dim firstofst As string = Mid(mystr,1,startpos-1)
Dim thetwo As String = Mid(mystr, startpos+ 1, 2)
Dim newString As String = firstofst+thetwo+" "+Mid(mystr,startpos,mystr.length - startpos)
startpos = startpos + 3
returnString(newString, startpos)
End If
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.
-
Mar 14th, 2008, 10:37 PM
#4
Thread Starter
Hyperactive Member
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??
-
Mar 14th, 2008, 11:21 PM
#5
Frenzied Member
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
-
Mar 14th, 2008, 11:35 PM
#6
Thread Starter
Hyperactive Member
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.
-
Mar 14th, 2008, 11:40 PM
#7
Thread Starter
Hyperactive Member
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
-
Mar 15th, 2008, 12:01 AM
#8
Thread Starter
Hyperactive Member
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.
-
Mar 15th, 2008, 01:05 AM
#9
Thread Starter
Hyperactive Member
Re: Adding space every two digits or letters
I think i solve it with this:
vb Code:
Dim stringpos As Integer = 1 Dim i As Short For i = 1 To Len(RTB2.Text) Dim curstring As String = Mid(RTB2.Text, stringpos, 2) RTB3.Text = RTB3.Text & curstring & " " stringpos = stringpos + 2 Next RTB2.Text = RTB3.Text RTB3.Text = vbNullString
-
Mar 15th, 2008, 01:11 AM
#10
Frenzied Member
Re: Adding space every two digits or letters
sorry about that mess. this works just fine.
vb Code:
Function returnString(ByVal mystr As String) As String
If mystr.Length < 2 Then
Return mystr
End If
Dim i As Integer = 0
Dim counter As Integer = 0
Dim newString As String = ""
While i < mystr.Length
newString = newString + mystr.Chars(i).ToString
i = i + 1
counter = counter + 1
If counter = 2 Then
newString = newString + " "
counter = 0
End If
End While
Return newString
End Function
Justin Fox
-
Mar 15th, 2008, 01:14 AM
#11
Frenzied Member
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
-
Mar 15th, 2008, 01:50 AM
#12
Thread Starter
Hyperactive Member
Re: Adding space every two digits or letters
That worked very nice also.
Thank you very much!
-
Mar 15th, 2008, 04:03 PM
#13
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
|