Blog posts about nils

  • Use default values in Javascript

    January 28, 2011 | Tags: Javascript, Nils, Snippet
    This cool way to use default values for javascript function parameters was provided by Nils:

    <script>

    function(param1) {
    param1 = param1 || "my default value";
    }

    </script>

    Update: Fixed typo in the script above (| should have been ||), thanks Nathan for pointing that out! Took the opportunity to add an alternative way to implement below:
    <script>

    function(param1) {
    param1 = typeof param1 !== 'undefined' ? param1 : "my default value";
    }

    </script>

  • Extract image tags from HTML in C#

    February 25, 2010 | Tags: C#, Regular expressions, Nils, Snippet
    A pretty little C# function that returns a list of strings containing all image tags extracted from a HTML string.
    private List<string> GetImagesInHTMLString(string htmlString) {
    List<string> images = new List<string>();
    string pattern = @"<(img)\b[^>]*>";

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for(int i=0, l=matches.Count; i<l; i++) {
    images.Add(matches[i].Value);
    }

    return images;
    }

  • Extend string with own methods

    November 12, 2009 | Tags: ASP.NET, C#, Nils, Snippet
    Here's a nice little sample submitted by Nils Forsman on how to extend the String object.
    public static class StringExtension
    {
    public static String AddSlashAfter(this String input)
    {
    return String.Concat(input, @"\");
    }
    public static String AddSlashBefore(this String input)
    {
    return String.Concat(@"\", input);
    }

    public static String Left(this String input, Int32 length)
    {
    if (length > input.Length)
    {
    return input;
    }
    return input.Substring(0, length);
    }

    public static String Right(this String input, Int32 length)
    {
    if (length > input.Length)
    {
    return input;
    }
    Int32 StartPosition = input.Length - length;
    return input.S
    return input.Substring(StartPosition, length);
    }

    public static String Mid(this String input, Int32 start, Int32 length)
    {
    if (start > input.Length)
    {
    return String.Empty;
    }
    if ((start + length) > input.Length)
    {
    return input.Substring(start);
    }
    return input.Substring(start, length);
    }
    }

    You'll then able to access your new methods on any string object, for example filePath.AddSlashAfter()

  • Remove in javascript string

    November 02, 2009 | Tags: Javascript, Regular expressions, Nils, Snippet
    Yet another extension to the String object in Javascript submitted by Nils Forsman. This one adds the function of removing a part of a string, given a startposition and the length of the part to remove:
    //A - Startindex
    //B - Length
    String.prototype.remove = function(A, B) {
    var s = '';
    if (A > 0)
    s = this.substring(0, A);
    if (A + B < this.length)
    s += this.substring(A + B, this.length);
    return s;
    };
    And some test code:
    var test = "test string test ";
    alert(test);
    var result = test.remove(12,3);
    alert(result);

  • Extend String with trim functions

    November 02, 2009 | Tags: Javascript, Regular expressions, Nils, Snippet
    This is a neat Javascript extension to the String object contributed by Nils Forsman.

    Here's the code that adds trim, ltrim and rtrim to the String object:
    String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
    };

    String.prototype.ltrim = function() {
    return this.replace(/^\s*/g, '');
    };

    String.prototype.rtrim = function() {
    return this.replace(/\s*$/g, '');
    };
    And a little code snippet to test the above functions:
    var testString = " test ";
    testString = testString .trim();
    alert("#" + testString + "#");