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

时间:2023-03-09 03:10:54
[转]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###