2016-05-02 73 views
0

我認爲這How to find the character after second vowel in a string in EXCEL?是有趣的,除了太寬泛。如何找到字符串中的第二個元音後的字符

加我回答了,但爲時已晚。

問題

例如,如果我的字符串是

快樂歌

輸出應該

頁。

如果它是空白的,則顯示 「無」

可能的解決方案

一式的方法(禮貌tikkaty

=IF(A1<>"",IFERROR(MID(A1,FIND("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a","|"),"e","|"),"i","|"),"o","|"),"u","|"),FIND("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a","|"),"e","|"),"i","|"),"o","|"),"u","|"))+1)+1,1),"no second vowel found"),"")

回答

1

這裏有一個免費的正則表達式變:

Function charTest(s As String) As String 
    Dim i As Integer, j As Integer, r As String 
    For i = 1 To Len(s) 
     If UCase(Mid(s, i, 1)) Like "[AEIOU]" Then j = j + 1 
     If j >= 2 Then Exit For 
    Next i 

    If j >= 2 And i < Len(s) Then r = Mid(s, i + 1, 1) 
    If r = "" Then r = "Nothing" 

    charTest = r 
End Function 

驗證:

Sub test2() 
    Debug.Print charTest("abcdefghijkl") 
    Debug.Print charTest("Excellence") 
    Debug.Print charTest("Animal") 
    Debug.Print charTest("The guppy pond") 
    Debug.Print charTest("The smallest bike") 
    Debug.Print charTest("Shea") 
End Sub 

˚F


p

沒什麼

3

一種可能的VBA UDF解決方案:

測試代碼

Sub test() 
Debug.Print StrOut("The Happy Song") 
Debug.Print StrOut("The Gypsy") 
Debug.Print StrOut("1234") 
End Sub 

UDF

Function StrOut(strIn As String) As String 
Dim objRegex As Object 
Dim objRegexM As Object 
Set objRegex = CreateObject("vbscript.regexp") 
With objRegex 
    .Pattern = "[aeiou].*?[aeiou](.)" 
    If .test(strIn) Then 
     Set objRegexM = .Execute(strIn) 
     strOut = objRegexM(0).submatches(0) 
    Else 
     StrOut = "Nothing" 
    End If 
End With 
End Function 
+1

添加'.IgnoreCase = TRUE',否則諸如 「動物」 和 「優秀」(或與任何字大寫元音)得到不正確的結果。 – Vegard

+0

@vegard不確定這將是不正確的,因爲沒有指定 - 雖然指出它有用 – brettdj

相關問題