0

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?

Adam
  • 6,041
  • 36
  • 120
  • 208

2 Answers2

1

You can do something like that:

public static string ReplaceEx(this string original, string pattern, string replacement)
{
    if (String.IsNullOrEmpty(pattern))
        return original;
    else
        return original.Replace(pattern, replacement);
}

and use the ReplaceEx in place of Replace as

newUrl = Request.RawUrl.ToString.ReplaceEx(Server.UrlEncode(category), "")
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks. I'm doing multiple replace statement, so instead of restructuring my code, I'd rather replace all .tostring.replace methods with .tostring.replaceex methods. With your code that's not possible right? How can I make that happen? – Adam May 12 '13 at 22:55
  • @Floran Place that code inside a global static class, then use the ReplaceEx, in the place or Replace on your code. Make a test and you see. – Aristos May 12 '13 at 22:59
  • I placed it inside a public class. How do I declare a global static class in vb.net? I did a Google search (found this:http://stackoverflow.com/questions/135841/marking-a-class-static-in-vb-net), changed it to: 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 but that didn't help. – Adam May 12 '13 at 22:59
  • @Floran well, I believe that you can place it in any class (not only static). Make a test – Aristos May 12 '13 at 23:00
  • @Floran Yes I see that, you make an auto convert (?) from c# to vb. Now this code if you include it on your code, somewhere global, it will make available the ReplaceEx. – Aristos May 12 '13 at 23:05
  • I did use an auto converter. I added the code to my Public GlobalFunctions class. However it does not extend the String object. I also fail to see where in your code it's defined that the ReplaceEx method should extend the String object? It only becomes available as a normal method, so via Globalfunctions.ReplaceEx...but that's not what I need. – Adam May 12 '13 at 23:10
  • @Floran before write it here I tested it and it work to me, but I can not help you on VB. – Aristos May 12 '13 at 23:16
0

This is to get it to work:

Module MyExtensions
<Extension()>
Public Function ReplaceEx(ByVal original As String, ByVal pattern As String, ByVal replacement As String) As String
    If [String].IsNullOrEmpty(pattern) Then
        Return original
    Else
        Return original.Replace(pattern, replacement)
    End If
End Function
End Module

Make sure that this line is in the code:

Imports System.Runtime.CompilerServices
Adam
  • 6,041
  • 36
  • 120
  • 208