1

I need to write a function "Elimination" which has arguments S as string K1, K2 characters. The function needs to return a string which is obtained when you remove characters K1 and K2.

So if I forward to function ("Odgovornost","o",s") it returns "Odgvrnt".

I tried:

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
4
  • 2
    You cannot set mid() to "", and you cannot refer to a variable by providing its name as a string literal. Simply call Replace() twice on S. If you want to use mid() anyway, declare a buffer of original length of S, loop over S and only copy the current character into the buffer (with mid() = ) if it does not equal K1 or K2. Commented Dec 22, 2018 at 21:19
  • How did you mean to use Replace twice on S I didn't understand you very well sorry Commented Dec 22, 2018 at 21:21
  • 1
    Replace(Replace(S, K1, ""), K2, ""). Commented Dec 22, 2018 at 21:22
  • I gave a try, but doesn't work. :( I want just to eliminate char which is exact the same as K1 or K2. Looks pretty simple but cannot make it work Commented Dec 22, 2018 at 21:28

1 Answer 1

1

Remove Characters

Replace Version

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

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

  Eliminacija = S

End Function

Mid Version

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.