ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

TakeFirst

Returns the first X characters from a string.

Source

public static string TakeFirst(this string s, int num)
{
    if(string.IsNullOrEmpty(s))
        return s;

    return (s.Length < num ? s : s.Substring(0, num));
}

Example

string someString = "Awesome";
string firstThree = "Awesome".TakeFirst(3);
//firstThree now is the string "Awe".

Author: Robert M. Downey

Submitted on: 18 okt. 2010

Language: C#

Type: System.String

Views: 5588