C#中英文混合字符串截取函数
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下://////截断字符串//////最大长度///原字符串///publicstaticstringCutStr(intma...
复制代码 代码如下:
/// <summary>
/// 截断字符串
/// </summary>
/// <param name="maxLength">最大长度</param>
/// <param name="str">原字符串</param>
/// <returns></returns>
public static string CutStr(int maxLength, string str)
{
string temp = str;
if (Regex.Replace(temp, "[u4e00-u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength)
{
return temp;
}
for (int i = temp.Length; i >= 0; i--)
{
temp = temp.Substring(0, i);
if (Regex.Replace(temp, "[u4e00-u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength - 3)
{
return temp + "...";
}
}
return "...";
}