-
Jan 11th, 2011, 11:40 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Adding dots between numbers
Hi,
I would like a string like this "0 1 2 3" to become "0.1.2.3" and to have that only for numbers, not strings.
My idea would be to ... hmm ... do a for , next loop to the len of the string...and while doing the loop to get the mid on each chr .... and if a chr = a number + empty space to replace the numer and empty spaec with .
would that be the correct way to go? any ideas?
Any help would be appreciated.
Cheers!
Thanks for helping me out.
-
Jan 11th, 2011, 11:49 AM
#2
Re: Adding dots between numbers
Perhaps using Replace function will give you what you need:
replace("0 1 2 3", space(1), ".")
-
Jan 11th, 2011, 12:28 PM
#3
Re: Adding dots between numbers
An interesting way to do it:
Code:
Option Explicit
Private Sub Form_Load()
Dim I As Long, J As Long, S As String, SR As String
Dim Text As String
Text = "Hello 1 2 3 yay."
S = "0 0"
For I = 0 To 9
Mid$(S, 1, 1) = ChrW$(48 + I)
For J = 0 To 9
Mid$(S, 3, 1) = ChrW$(48 + J)
If InStr(Text, S) Then
SR = S
Mid$(SR, 2, 1) = "."
Text = Replace(Text, S, SR)
End If
Next J
Next I
MsgBox Text
Unload Me
End Sub
Next thing to do: figure out how & why it works
-
Jan 11th, 2011, 12:42 PM
#4
Thread Starter
Hyperactive Member
Re: Adding dots between numbers
Originally Posted by RhinoBull
Perhaps using Replace function will give you what you need:
replace("0 1 2 3", space(1), ".")
this method replaces all the empty spaces...not only the ones between the numbers.
Originally Posted by Merri
An interesting way to do it:
Code:
Option Explicit
Private Sub Form_Load()
Dim I As Long, J As Long, S As String, SR As String
Dim Text As String
Text = "Hello 1 2 3 yay."
S = "0 0"
For I = 0 To 9
Mid$(S, 1, 1) = ChrW$(48 + I)
For J = 0 To 9
Mid$(S, 3, 1) = ChrW$(48 + J)
If InStr(Text, S) Then
SR = S
Mid$(SR, 2, 1) = "."
Text = Replace(Text, S, SR)
End If
Next J
Next I
MsgBox Text
Unload Me
End Sub
Next thing to do: figure out how & why it works
This is what i have been looking for...im gonna play with it to figure it out.
Thank both for your time and help.
Thanks Merri, rating you now
Thanks for helping me out.
-
Jan 11th, 2011, 02:48 PM
#5
Re: [RESOLVED] Adding dots between numbers
Merri was just playing around, btw. His solution is interesting but inefficient. It's fine for a one-off routine, like if it gets called in response to user action. But if you need to call this within an intensive loop you may want to hold out for a speedier version.
-
Jan 11th, 2011, 03:29 PM
#6
Re: [RESOLVED] Adding dots between numbers
This should be efficient enough but feel free to modify it:
Code:
Option Explicit
Private Function FormatString(ByVal strIn As String) As String
Dim arBytes() As Byte
Dim i As Integer
Dim tmp As String
arBytes = StrConv(strIn, vbFromUnicode)
For i = 0 To UBound(arBytes)
If arBytes(i) >= 48 And arBytes(i) <= 57 Then
tmp = tmp & Chr(arBytes(i)) & "."
End If
Next i
If VBA.Right$(tmp, 1) = "." Then
tmp = VBA.Left$(tmp, Len(tmp) - 1)
End If
FormatString = tmp
End Function
Private Sub Command1_Click()
Debug.Print FormatString("1 2 3 4 5 6 7")
Debug.Print FormatString("1a2b3c4d5e6fgh7")
End Sub
-
Jan 11th, 2011, 03:31 PM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] Adding dots between numbers
Yes, well i need it like in 500 listview add item ... to do the operation ....
ill try the Rhino method too.. thanks, rated
Thanks for helping me out.
-
Jan 11th, 2011, 03:51 PM
#8
Re: [RESOLVED] Adding dots between numbers
Ellis Dee: another interesting part is that you can remove Replace function and just use InStr and Mid$ all the way, which will be more efficient than calls to Replace. You won't be creating a new string, although you'll still loop through the whole string a good few times using InStr.
Another solution would be to look for all the spaces using InStr and then simply check whether the last and next character is a number. This would mean a single loop through all the characters in the string using InStr but regular VB methods to get the character codes would be a Mid$ & AscW combo, which may not be ideal. Much better than the previous suggestion though.
A truly speedy solution would be safe array hacking the way through the array. A single loop through it all would do the job.
Anyway, I guess what I posted can be counted as somewhat valuable learning material
-
Jan 11th, 2011, 03:54 PM
#9
Thread Starter
Hyperactive Member
Re: [RESOLVED] Adding dots between numbers
Was of great help. I would have done it in a brilliantly awful way with tons of code
Thanks for helping me out.
-
Jan 11th, 2011, 04:04 PM
#10
Re: [RESOLVED] Adding dots between numbers
Perhaps a boringly straightforward way to do it is:
Code:
Option Explicit
Private Function AddDots(ByVal Text As String) As String
Dim I As Long
Do
I = InStr(I + 1, Text, " ")
If I > 1 Then If Mid$(Text, I - 1, 1) Like "#" Then Mid$(Text, I, 1) = "."
Loop While I > 0
AddDots = Text
End Function
Private Sub Form_Load()
Show
Print "1 2 3 4 5", AddDots("1 2 3 4 5")
Print "1 2 3 4 5", AddDots("1 2 3 4 5")
Print "", AddDots("")
Print "a", AddDots("a")
Print "abc 123", AddDots("abc 123")
Print "abc 123 ", AddDots("abc 123 ")
Print " abc 123 ", AddDots(" abc 123 ")
End Sub
-
Jan 11th, 2011, 04:10 PM
#11
Re: [RESOLVED] Adding dots between numbers
dilettante: that will of course replace space with a dot after each number.
Here is what I suggested in post #8:
Code:
Option Explicit
Public Function NumbersFromSpaceToDot(Text As String) As String
Dim C As Integer, I As Long, L As Long
NumbersFromSpaceToDot = Text
I = InStr(2, Text, " ")
L = Len(Text)
Do While I > 0 And I < L
C = AscW(Mid$(Text, I - 1, 1)) - 48
If C >= 0 And C <= 9 Then
C = AscW(Mid$(Text, I + 1, 1)) - 48
If C >= 0 And C <= 9 Then
Mid$(NumbersFromSpaceToDot, I, 1) = "."
End If
End If
I = InStr(I + 1, Text, " ")
Loop
End Function
Private Sub Form_Load()
MsgBox NumbersFromSpaceToDot(" A B C 0 1 2 3 4 5 D E F ")
Unload Me
End Sub
Not as short'n'neat, but it checks numbers from both sides of the space and if I recall correctly Mid$ + AscW is a faster pair than Like. But I guess the most important part is to reduce InStr loop to one full run through all characters.
Edit!
Here is an interesting twist to dilettante's function using IsNumeric instead of Like:
Code:
Public Function SpaceDotShort(Text As String) As String
Dim I As Long
SpaceDotShort = Text
Do
If I > 1 Then If IsNumeric(Mid$(Text, I - 1, 3)) Then Mid$(SpaceDotShort, I, 1) = "."
I = InStr(I + 1, Text, " ")
Loop While I > 0 And I < Len(Text)
End Function
Last edited by Merri; Jan 11th, 2011 at 04:27 PM.
-
Jan 11th, 2011, 06:53 PM
#12
Re: [RESOLVED] Adding dots between numbers
Originally Posted by batori
Yes, well i need it like in 500 listview add item ... to do the operation ....
That's small enough that all the solutions posted would happen in the blink of an eye, so use whichever one appeals to you the most or that you understand the best.
By intensive loop I meant tens of thousands of iterations.
-
Jan 11th, 2011, 07:29 PM
#13
Thread Starter
Hyperactive Member
Re: [RESOLVED] Adding dots between numbers
I have chosen the one that i understand the best.
The others seem to complicated for me
A big thanks to all of you guys!!!!
Cheers!!
Thanks for helping me out.
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
|