Quantcast
Channel: VB.Classic
Viewing all articles
Browse latest Browse all 9

Counting Unique Characters in a String

$
0
0

I noticed this post at vbCity and after looking at the code wondered if I could come up with a more compact solution and I think I have.

Private Function FindNumberOfUniqueCharactersInString(ByVal strVvText As String _
                                                     , ByVal blnVvCaseSensitive As Boolean _
                                                     ) As Integer
    Dim intLvIndex As Integer
    Dim intLvCount As Integer: intLvCount = 1
    strVvText = IIf(blnVvCaseSensitive, strVvText, UCase$(strVvText))
    For intLvIndex = 2 To Len(strVvText)
        If InStr(Left(strVvText, intLvIndex - 1) _
                , Mid(strVvText, intLvIndex, 1) _
                ) = 0 Then
            intLvCount = intLvCount + 1
        End If
    Next
    FindNumberOfUniqueCharactersInString = intLvCount
End Function

There are possibly even more compact and elegant solutions to this interesting little issue but I quite like my solution :)


Viewing all articles
Browse latest Browse all 9

Trending Articles