gpt4 book ai didi

excel - 从字母数字字符串中检索字母字符

转载 作者:行者123 更新时间:2023-12-04 21:08:13 26 4
gpt4 key购买 nike

怎么分手AB2468123

我尝试了以下方法:

myStr = "AB2468123"
split(myStr, "1" OR "2" OR "3"......."9")

我只想得到字母(字母)。

谢谢。

最佳答案

如何仅从输入字符串中检索字母:

Function GetLettersOnly(str As String) As String
Dim i As Long, letters As String, letter As String

letters = vbNullString

For i = 1 To Len(str)
letter = VBA.Mid$(str, i, 1)

If Asc(LCase(letter)) >= 97 And Asc(LCase(letter)) <= 122 Then
letters = letters + letter
End If
Next
GetLettersOnly = letters
End Function

Sub Test()
Debug.Print GetLettersOnly("abc123") // prints "abc"
Debug.Print GetLettersOnly("ABC123") // prints "ABC"
Debug.Print GetLettersOnly("123") // prints nothing
Debug.Print GetLettersOnly("abc123def") // prints "abcdef"
End Sub

编辑:为了完整性(和 Chris Neilsen)这里是 Regex方法:
Function GetLettersOnly(str As String) As String
Dim result As String, objRegEx As Object, match As Object

Set objRegEx = CreateObject("vbscript.regexp")

objRegEx.Pattern = "[a-zA-Z]+"
objRegEx.Global = True
objRegEx.IgnoreCase = True

If objRegEx.test(str) Then
Set match = objRegEx.Execute(str)
GetLettersOnly = match(0)
End If
End Function

Sub test()
Debug.Print GetLettersOnly("abc123") //prints "abc"
End Sub

关于excel - 从字母数字字符串中检索字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com