I\'d like to write an extension method to the String class so that if the input string to is longer than the provided length N, only the first
String
N
You can use LINQ str.Take(n) or str.SubString(0, n), where the latter will throw an ArgumentOutOfRangeException exception for n > str.Length.
str.Take(n)
str.SubString(0, n)
ArgumentOutOfRangeException
n > str.Length
Mind that the LINQ version returns a IEnumerable, so you'd have to convert the IEnumerable to string: new string(s.Take(n).ToArray()).
IEnumerable
string
new string(s.Take(n).ToArray())