naungsai
09-09-2008, 09:01 PM
Dear Friends
I want to group same characters in a string.
My current string
111222334467
2255
556677
(The string value are stored as text. The character in a string is unlimited)
I want to get
123467
25
567
How should i get it?
Thanks in advance.
Sai :confused:
raskew
09-10-2008, 03:43 AM
Hi -
Try copying/pasting this to a standard module, then test from debug (immediate) window as shown:
Public Function UniqString(ByVal pstr As String) As String
'Purpose: Extract distinct values from a string
'Input: ? UniqString("111222334467")
'Output: 123467
Dim strHold As String
Dim n As Integer
For n = 1 To Len(pstr)
strHold = strHold & IIf(InStr(strHold, Mid(pstr, n, 1)) = 0, Mid(pstr, n, 1), "")
Next n
UniqString = strHold
End Function
HTH - Bob
namliam
09-10-2008, 04:06 AM
Kudos on that code bob... Getting it all in one line....
however it will break (I think) if the value going in is
222211111122222223333333
it will return 213 instead of 2123 what the OP want
The fix for that is this:
Public Function UniqString(ByVal pstr As String) As String
'Purpose: Extract unique values from a string
'Input: ? UniqString("111222334467")
'Output: 123467
Dim strHold As String
Dim n As Integer
strHold = Left(pstr, 1)
For n = 1 To Len(pstr)
' add the character only if it is not equal to the previous one
strHold = strHold & IIf(Right(strHold, 1) <> Mid(pstr, n, 1), Mid(pstr, n, 1), "")
Next n
UniqString = strHold
End Function
raskew
09-10-2008, 05:44 AM
Hi -
We're obviously attacking this from different points of view. My intent was to include only unique integers, regardless of the order in which they were presented.
Guess we'll have to hear from the OP.
Best Wishes - Bob
naungsai
09-10-2008, 06:29 PM
Dear Bob and Mailman
Thanks for your quick response and effective codes. Both codes work. I have already got the code, which i find in this forum, to sort the characters in the string.
I do admire your work.:D
Thanks again.
Sai Kyaw Han :)