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

ToStringLimit(limit)

Save values checking length

Source

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

  return str;
}

Example

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: 2496