Results 1 to 3 of 3

Thread: [RESOLVED] How to capture the first letter in a not-repeating string ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Location
    http://bbat.forumeiro.com/
    Posts
    86

    Resolved [RESOLVED] How to capture the first letter in a not-repeating string ?

    Hi
    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 retuned 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 !


  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

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

    Try this one
    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



  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

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

    Yet another way:

    Code:
    Private Function FindFirstNonRepeatingChar(ByRef Text)
        Dim Pos, Length
    
        Pos = 1
        Length = Len(Text)
    
        Do While Mid(Text, Pos, 1) = Mid(Text, Pos + 1, 1) And Pos < Length
            Do:  Pos = Pos + 1
            Loop While Mid(Text, Pos, 1) = Mid(Text, Pos + 1, 1) And Pos < Length
            Pos = Pos + 1
        Loop
    
        FindFirstNonRepeatingChar = Mid(Text, Pos, 1)
    End Function
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width