fluentsharp / FluentSharp

Fluent API for the .NET Framework (used by the O2 Platform)
63 stars 18 forks source link

Null Handling - on String ExtensionMethods: remove and replaceAllWith #8

Closed DinisCruz closed 10 years ago

DinisCruz commented 10 years ago

The remove function is affected: https://github.com/o2platform/FluentSharp/blob/master/FluentSharp.CoreLib/ExtensionMethods/System/String_ExtensionMethods.cs#L240

With the root cause being: https://github.com/o2platform/FluentSharp/blob/master/FluentSharp.CoreLib/ExtensionMethods/System/String_ExtensionMethods.cs#L255

This replace method is doing it right: https://github.com/o2platform/FluentSharp/blob/master/FluentSharp.CoreLib/ExtensionMethods/System/String_ExtensionMethods.cs#L248

Here is an example of a Razor page that had was affected by this issue:

    @foreach (var item in TBot_Brain.AvailableScripts)
    {     
        var hasTemplate = TBot_Brain.TemplateService.HasTemplate(item.Value); 
        @: [@hasTemplate] @item.Key : @item.Value.remove(webRoot)     
        {
            if (hasTemplate) 
             { 
                TBot_Brain.TemplateService.RemoveTemplate(item.Value);
             }
        }
    }
DinisCruz commented 10 years ago

fixed with:

        public static string    replaceAllWith(this string targetString, string stringToReplaceWith, params string[] stringsToFind)
        {            
            if(targetString.valid() && stringToReplaceWith.valid() && stringsToFind.notEmpty())
                foreach (var stringToFind in stringsToFind)
                    targetString = targetString.Replace(stringToFind, stringToReplaceWith);
            return targetString;
        }