一个简单的字符串截取函数,可以指定截取位数,例如:输入(“This is way too long”, 11, “…”) 返回为 “This is way…”
public static string Truncate(this string myString, int limit, string symbol)
{
if (myString == null)
return null;
if (limit < 0)
throw new ArgumentOutOfRangeException("limit", limit, "must be 0 or greater");
if (symbol == null)
throw new ArgumentNullException("symbol must not be null");
if (myString.Length < limit)
return myString;
return myString.Substring(0, limit) + symbol;
}
