字符串字符删除

时间:2018-12-22 21:06:59

标签: excel vba

我需要编写一个函数“ Elimination”,其参数S为字符串K1,K2。该函数需要返回一个字符串,该字符串是在删除字符K1和K2时获得的。

因此,如果我转发功能(“ Odgovornost”,“ o”,s”),它将返回“ Odgvrnt”。

我尝试过:

Function Eliminacija(S As String, K1 As String, K2 As String)
Dim I As Integer
For I = 1 To Len(S)

If Mid(S, I, 1) Like "K1" Or Mid(S, I, 1) Like "K2" Then
Mid(S, I, 1) = ""

End If
Next
Elimination = S

1 个答案:

答案 0 :(得分:1)

删除字符

替换版本

Function Eliminacija(S As String, K1 As String, K2 As String)

  S = Replace(S, K1, "")
  S = Replace(S, K2, "")

  Eliminacija = S

End Function

中间版本

Function Eliminacija(S As String, K1 As String, K2 As String)

  Dim i As Integer
  Dim str As String

  For i = 1 To Len(S)

    If Not Mid(S, i, 1) Like K1 And Not Mid(S, i, 1) Like K2 Then
      str = str & Mid(S, i, 1)
    End If

  Next

  Eliminacija = str

End Function