2012-02-07 120 views
1

刪除字符我有以下的功能,但我想它做相反如返回壞字符不是那些我指定從字符串

此功能允許您指定兩個字符串。要分析的字符串和字符的字符串,你想從指定的第一個字符串保存 - 將返回編輯串

Function GETALPHANUMERIC(text, str_all) 

For lenstr = 1 To Len(text) 
    If InStr(str_all, LCase(Mid(text, lenstr, 1))) Then 
     GETALPHANUMERIC = GETALPHANUMERIC & Mid(text, lenstr, 1) 
    End If 
Next 

End Function 

感謝 羅布

回答

2

只需添加= 0

If InStr(str_all, LCase(Mid(text, lenstr, 1))) = 0 Then 

時沒有找到匹配的InStr函數返回0。

+0

完美謝謝! – Rob 2012-02-07 14:51:48

1

您可以使用拆分功能像下面的例子。

Sub test() 

    Dim s As String 
    Dim result As String 

    s = "abcXdefXghiX" 
    result = excludeCharacter(s, "X") 

    MsgBox s & " excluding X is " & result 

End Sub 

Function excludeCharacter(originalString As String, exclude As String) As String 

    Dim sArray() As String 
    Dim result As String 
    Dim i As Long 

    sArray = Split(originalString, exclude) 

    For i = 0 To UBound(sArray, 1) 
    result = result & sArray(i) 
    Next i 

    excludeCharacter = result 

End Function 
+0

這與OP所要求的相反。 (OP已具有此功能。) – 2012-02-07 14:42:47

0

而且剛纔寫的太 - 想我會分享只是櫃面

Function BadChar(text, str_all) 

    For lenstr = 1 To Len(text) 
    If InStr(str_all, LCase(Mid(text, lenstr, 1))) = 0 Then 
     BadChar = BadChar & Mid(text, lenstr, 1) 
    End If 
Next 

End Function 
0

換句話說,你想去掉(刪除)從源字符串中的字符?如果是的話,這裏有一個答案:

Function StripChars(source As String, chars As String) 
    Dim pos As Long 
    For i = 1 To Len(chars) 
     Do 
      pos = InStr(1, source, Mid(chars, i, 1), VbCompareMethod.vbTextCompare) 
      If pos > 0 Then 
       source = Mid(source, 1, pos - 1) & Mid(source, pos + 1) 
      Else 
       Exit Do 
      End If 
     Loop 
    Next i 
    StripChars = source 
End Function