I have this codeline:
newUrl = Request.RawUrl.ToString.Replace(Server.UrlEncode(category), "")
When category="" I get the error: String cannot be of zero length. Parameter name: oldValue
So I want to add a new replace method, that does except an empty string as parameter to replace. I don't want to check if category is an empty string, since the check needs to happen a lot and will make my code too complex. So I want to add a method "ReplaceNew" or overload the "Replace" method of the String object with an extra parameter "ignoreEmptyValue".
I tried:
Public Module CustomMethods
Public Function ReplaceEx(original As String, pattern As String, replacement As String) As String
If [String].IsNullOrEmpty(pattern) Then
Return original
Else
Return original.Replace(pattern, replacement)
End If
End Function
End Module
And
Public Class GlobalFunctions
Public Shared Function ReplaceEx(original As String, pattern As String, replacement As String) As String
If [String].IsNullOrEmpty(pattern) Then
Return original
Else
Return original.Replace(pattern, replacement)
End If
End Function
End Class
I then tried:
newUrl = Request.RawUrl.ToString.ReplaceEx(Server.UrlEncode(category), "")
But the ReplaceEx method is not available this way, I get the error:
'ReplaceEx' is not a member of 'String'.
I really want to have the ReplaceEx method available on the String object, since I don't want to restructure my code, but rather replace all .ToString.Replace methods with .ToString.ReplaceEx methods. How can I do so?