[转]c#截取指定长度的字符串

/// <summary>
/// 截取指定長度的字符串
/// </summary>
/// <param name="s"></param>
/// <param name="len"></param>
/// <param name="style"></param>
/// <returns></returns>
private string GetStrLenAll(string s, int len, string style)
{
string temp = s;
if (Regex.Replace(temp, "[^\x00-\xff]", "zz", RegexOptions.IgnoreCase).Length <= len)
{
return temp;
}
for (int i = temp.Length; i >= 0; i--)
{
temp = temp.Substring(0, i);
if (Regex.Replace(temp, "[^\x00-\xff]", "zz", RegexOptions.IgnoreCase).Length <= len - style.Length)
{
return temp + style;
}
}
return "";
}

 

来源:http://wenku.baidu.com/link?url=87hx6gFRuPuXqNs-K4s0JxsMWEQlUQ4XlNtEfWDkiFLy1EEExkCJvaYdsJN6EbzJZPf1tQcbeoWq4MMn09JBSwjLAPx3ghTUsmIENSkmZyS###

posted @ 2014-03-04 09:28  Seasons1987  阅读(1275)  评论(0编辑  收藏  举报