Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to capture the first letter in a not-repeating string ?

Status
Not open for further replies.

crackoo

Programmer
Feb 17, 2011
132
TN
Hi [peace]
I want to create a function for capturing the first letter in a not-repeating string
For example if have this :
MyString = "aabbccddMffrryx"
So the first letter will be returned as M
So i'm looking for the best and the easy way to do it in vbscript.
So i ask you how can i do this function ?
Thank you !
 
Pure VBscript solution:
Code:
[blue]    mystring = "aabbcccddMffrryx"
    NoMatch = True
    For lp = 1 To Len(mystring) - 1
        current = Mid(mystring, lp, 1)
        nextChar = Mid(mystring, lp + 1, 1)
        If current <> nextChar And NoMatch Then
            MsgBox current
            Exit For
        ElseIf current <> nextChar And lp = Len(mystring) - 1 Then
            MsgBox nextChar
        ElseIf current <> nextChar Then
            NoMatch = True
        Else
            NoMatch = False
        End If
    Next[/blue]

VBscript with regular expression solution:
Code:
[blue]    mystring = "aabbccddMffrryx"
    With CreateObject("vbscript.regexp")
        .Global = True
        .Pattern = "(.)\1+"
         MsgBox Left$(.Replace(mystring, ""), 1)
    End With[/blue]
 
Hi [peace] and thank you Strongm for your reply !
So, Your solution works, but when i choose a string like MyString = "www.google.com" so it return me "." instead of "l"
So i found this trick to get it working like this :

Code:
Private Function GetFirstSingleLetter(ByVal strText As String) As String
    Dim j As Long
    Dim r As String
    Dim c As String
    For j = 1 To Len(strText)
        c = Mid$(strText, j, 1)
        If InStr(j + 1, strText, c, vbTextCompare) = 0 And c <> vbTab Then
            r = c
            Exit For
        Else
            ' Replace repeated letter with vbTab in order to not find it again.
            strText = Replace(strText, c, vbTab, , , vbTextCompare)
        End If
    Next
    GetFirstSingleLetter = r
 End Function
 
OK, so you mean alphanumeric character rather than just characters. Fine, that's easily handled.

But I don' think your code does the trick: why would 'l' be the first non-repeated alphanumeric in " Surely that would be 'g'? And what if your text contains other punctuation marks? For example "'REM statement"

A tiny addition to my RegExp solution would seem to do the trick. And I'll leave my code as VBscript (you seem to have drifted over to VB):

Code:
[blue]Private Function Example(strText)
    With CreateObject("vbscript.regexp")
        .Global = True
        .Pattern = "(.)\1+|\W"
        Example = Left$(.Replace(strText, ""), 1)
    End With
 End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top