asp.net - add new string object method -


i have codeline:

newurl = request.rawurl.tostring.replace(server.urlencode(category), "") 

when category="" error: string cannot of 0 length. parameter name: oldvalue

so want add new replace method, except empty string parameter replace. don't want check if category empty string, since check needs happen lot , make code complex. want add method "replacenew" or overload "replace" method of string object parameter "ignoreemptyvalue".

i tried:

public module custommethods  public function replaceex(original string, pattern string, replacement string) string     if [string].isnullorempty(pattern)         return original     else         return original.replace(pattern, replacement)     end if end function end module 

and

public class globalfunctions  public shared function replaceex(original string, pattern string, replacement string) string     if [string].isnullorempty(pattern)         return original     else         return original.replace(pattern, replacement)     end if end function end class 

i tried:

newurl = request.rawurl.tostring.replaceex(server.urlencode(category), "") 

but replaceex method not available way, error:

'replaceex' not member of 'string'.

i want have replaceex method available on string object, since don't want restructure code, rather replace .tostring.replace methods .tostring.replaceex methods. how can so?

you can 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 replaceex in place of replace as

newurl = request.rawurl.tostring.replaceex(server.urlencode(category), "") 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -