RegEx.Replace isn't working

I have this:
Imports System.Text.RegularExpressions.Regex

Select Case True
Case CBool(GetAsyncKeyState(116))
With Options.Items
If .Item(0).ToString.Contains("OFF") Then
MsgBox("check")
Replace(.Item(0).ToString, "OFF", "ON")
Else
MsgBox("nocheck")
Replace(.Item(0).ToString, "ON", "OFF")
End If
End With
End Select

Options is a listbox.
Now, when I press F5 the msgbox says 'check' but the string does not change "OFF" to "ON".
Am I missing something



Answer this question

RegEx.Replace isn't working

  • Joe Kehnast

    Hi,

    I tested this code, which worked perfectly as desired.

    Imports System.Text.RegularExpressions.Regex

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim tmpString As String

    Dim tmpString_changed As String

    tmpString = TextBox1.Text

    tmpString_changed = Replace(tmpString, "ON", "OFF")

    MsgBox(tmpString_changed )

    End Sub

    End Class

    I believe you need to capture the return string for this purpose.

     

    Bye,

    Sugan



  • xplosiv_1

    Well, you're certainly using the wrong tool - the string class has a replace method.

    Have you set a breakpoint to make sure this is being called at all



  • Cmeyla

    That is what was wrong. I wasn't catching the returned string. I knew it had to be simple. thank you.

    Another thing, what event would I use to catch when the text changes in the listbox. I tried textchanged but it didn't work.


  • Rick Stephens

    Yes - you are right. Strings are immutable so methods that change them always return a string. Even if you use string.replace ( and you probably should ), this will continue to be the case.



  • Ranier

    If i am right, I think what you where trying for is "SelectedIndexChanged".

    Try this event.



  • RegEx.Replace isn't working