我正在尝试创建一个函数,该函数将从较长的混合大小写字符串返回一个大写部分。
我的地址是以Suburb为大写的字符串输入的,并且想要提取郊区和邮政编码
e.g。
strInput = "01 Street St SUBURB 1111"
desired function output = "SUBURB 1111"
请记住,有些郊区有两个字,例如“THAS SUBURB 2222”
这是我到目前为止所做的,但它不起作用。 任何帮助将不胜感激。
Function Suburb(strInput As String) As String
Dim strTest As String
Dim strEnd As Integer
Dim n As Integer, counter As Integer
'Count the characters in the string and set variable to string length - 5 (for postcode)
strSubEnd = Len(strInput) - 5
counter = 1
'Start Loop
For n = 1 To strEnd
'start from First Character and move to first space
n = InStr(counter, strInput, " ")
'create sting to test
strTest = Mid(strInput, n + 1, strSubEnd - n)
'check if string is upper case
If strTest = UCase(strTest) Then
Suburb = Mid(strInput, n + 1, Len(strInput) - n)
'Else increase counter and re-test
Else: counter = n
End If
Next
End Function
答案 0 :(得分:0)
使用split在空格上拆分字符串并测试它是否不是数字和大写:
Function Suburb(strInput As String) As String
Dim i As Integer
Dim spltStr() As String
spltStr = Split(strInput)
For i = 0 To UBound(spltStr)
If UCase(spltStr(i)) = spltStr(i) And Not IsNumeric(spltStr(i)) Then
Suburb = Suburb & " " & spltStr(i)
End If
Next i
Suburb = Trim(Suburb) & Right(strInput, 5)
End Function
答案 1 :(得分:0)
Dim strTest As String
Dim strEnd As Integer
Dim n As Integer
'Count the characters in the string and set variable to string length - 5 (for postcode)
strEnd = Len(strInput) - 5
counter = 1
'Start Loop
For n = 1 To strEnd
'start from First Character and check if it is a space
If Mid(strInput, n, 1) = " " Then
'create sting to test
strTest = Mid(strInput, n + 1, strEnd - n)
'check if string is upper case
If strTest = UCase(strTest) Then
Suburb2 = Mid(strInput, n + 1, Len(strInput) - n)
Exit For
'Else move to next character and re-test
End If
End If
Next n
End Function
答案 2 :(得分:0)
Public Function Suburb(strInput As String) As String
Dim RegExp As Object
Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "(([A-Z]+)\s)?([A-Z]+)\s([0-9]+)"
With .Execute(strInput)
If .Count <> 0 Then
Suburb = .Item(0).Value
End If
End With
End With
Set RegExp = Nothing
End Function