public static string CatChuoi(this string s, int length)
{
if (String.IsNullOrEmpty(s))
throw new ArgumentNullException(s);
var words = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (words[0].Length > length)
throw new ArgumentException("Từ đầu tiên dài hơn chuỗi cần cắt");
var sb = new StringBuilder();
foreach (var word in words)
{
if ((sb + word).Length > length)
return string.Format("{0}...", sb.ToString().TrimEnd(' '));
sb.Append(word + " ");
}
return string.Format("{0}...", sb.ToString().TrimEnd(' '));
}
Tuesday, July 16, 2013
Cắt n ký tự đầu tiên của một chuỗi với C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment