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


Save values checking length


public static string ToStringLimit(this String str, int limit) {
  if (str.Length > limit) {
    return str.Substring(0, limit);

  return str;


string test = "hello world";

// set hi to test value
// but maybe hi is being saved into the database and
// it only stores max length of 5.

string hi = test.ToStringLimit(5);

// hi = "hello"
// saves you having to first check length then using substr
// nice little extension helper :)

Author: Neil Steventon

Submitted on: 14 mrt 2015

Language: C#

Type: String

Views: 3065