VB.Net Replace not working?

2019-08-02 00:52发布

问题:

Not sure if I'm doing something wrong or not, basically my code starts at "111111111" and counts up by adding "1" to the original number every time the thread is able to. I want the method to skip 0's in the sequence though, instead of going to "111111120" after "111111119" I would like it to go straight to "111111121".

    Private Sub IncreaseOne()
    If count < 999999999 Then
        count += 1
    Else
        done = True
    End If
    If CStr(count).Contains("0") Then
        MsgBox("theres a 0 in that...darn.")
        CStr(count).Replace("0", "1")
    End If
    End Sub

*note, my message box displays when it is suppose to but, 0s are not changed to 1s

回答1:

Replace returns a string with the effects of the Replace, It doesn't work in place....
(Remember, in NET the strings are immutable objects)

Dim replaced = CStr(count).Replace("0", "1")

However you need to convert the string obtained to an integer and reassign to count.

count = Convert.ToInt32(replaced)


回答2:

Replace is a function that returns a sting.

In other words, you need a variable to hold the result, like this:

Dim newValue = CStr(count).Replace("0", "1")