天天看點

[轉]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###