VB.NET Replace(Replace(input, “”, “”), “”, “”) issue / quirk / bug
I ran into a very strange issue the other day. This was in VB.NET and as you’ll see in a moment is rather un-googleable. Someone checked in a change that essentially did this:
Dim strExample as String = "ab" Return Replace(Replace(strExample, "a", ""), "b", "") |
What do you think was returned?
I expected it to return empty string “” but for some reason it returned Nothing! (?!?)
Yet this returns empty string:
Dim strExample as String = "a" Return Replace(strExample, "a", "") |
So it appears that passing in Replace() as the first parameter to Replace() will convert empty string to Nothing. If the result isn’t empty string it’s fine, the issue is only when the result is empty string (which in the real world was only ever an issue if the input string was empty to begin with).
This works as expected:
Dim strExample as String = "ab" Return strExample.Replace("a", "").Replace("b", "") |
Crazy.