0

I have a problem replacing a certain string inside another string using a function. For some reason, the final string is still the same. Here is my code:

Private Function RemoveSomeChars(ByVal song As String)
    Dim finalSong As String = song
    If finalSong.ToLower.Contains("official") Then
        finalSong.ToLower.Replace("official", "")
    End If
    Return finalSong
End Function

And I use it like this:

MsgBox(RemoveSomeChars(currentSong))
2
  • 3
    .Replace() is a function returning a new string. You should also turn on Option Strict Commented Feb 18, 2016 at 2:07
  • 1
    "thanks" on SO would be to upvote the linked answer. The answer duplicated here too since it points out the possible issue with .ToLower(). Commented Feb 18, 2016 at 3:37

1 Answer 1

1

It's as Plutonix indicated in the comments; String.Replace() is a function that won't modify the underlying string but rather return a new string. In your case, you'd want to use it like this

finalSong = finalSong.ToLower.Replace("official", "")

String.ToLower() will behave in much of the same way.

As a final note (this may or may not be important to you but) I'd like to point out that if your if block is entered then finalSong will be converted to all-lowercase whereas it'll be unchanged in the event that the if block is skipped.

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.