Group characters in string

naungsai

Abecedarian
Local time
Tomorrow, 00:06
Joined
Sep 8, 2008
Messages
123
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:
 
Hi -

Try copying/pasting this to a standard module, then test from debug (immediate) window as shown:

Code:
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
 
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:
Code:
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
 
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
 
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 :)
 

Users who are viewing this thread

Back
Top Bottom