Get unique characters in string

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Get unique characters in string

Post by YasserKhalil »

Hello everyone

I am trying the following udf that gets the unique characters in string

Code: Select all

Function UniqueChars(txt As String)
    Dim x, i As Long
    With CreateObject("Scripting.Dictionary")
        x = Split(StrConv(Replace(txt, " ", ""), 64), Chr(0))
        For i = 0 To UBound(x) - 1
            .Item(x(i)) = Empty
        Next i
        UniqueChars = Join(.Keys, "")
    End With
End Function
The code is ok for English letters but not for Arabic letters. Any ideas?

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get unique characters in string

Post by HansV »

How about

Code: Select all

Function UniqueChars(txt As String)
    Dim x As String, i As Long
    x = Replace(txt, " ", "")
    With CreateObject("Scripting.Dictionary")
        For i = 1 To Len(x)
            .Item(Mid(x, i, 1)) = Empty
        Next i
        UniqueChars = Join(.Keys, "")
    End With
End Function
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Get unique characters in string

Post by YasserKhalil »

Amazing. Thank you very much.

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get unique characters in string

Post by HansV »

This version is a lot simpler, I don't know why you used Split and StrConv.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Get unique characters in string

Post by YasserKhalil »

I just have searched and found the previous UDF in the first post. I thought it will work with Arabic characters.