Monday, March 14, 2011

KnowDotNet: String.Insert()

Another function I don't think I've ever used before... I tend to use String.Format() to insert values into strings or just concatenate them so I'm not sure how much I'll use this.

using System;

namespace String_Insert
{
    class Program
    {
        static void Main( string [] args ) {
            string interesting = "This is interesting";

            var veryInteresting = interesting.Insert( 8, "very " );

            Console.WriteLine( interesting );
            Console.WriteLine( veryInteresting );
        }
    }
}

We start off in line 8 creating a string and then in line 10 we insert the word "very " into that string using the Insert() method of the interesting object. This returns a new string with the words "This is very interesting".

No comments:

Post a Comment