C#中英文混合字符串截取函数
2013-09-10 16:31
721 查看
代码一
/// <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 "..."; }
代码二
private string GetByteString(string center, int maxlen, string endStr) { string temp = center.Substring(0, (center.Length < maxlen + 1) ? center.Length : maxlen + 1); byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp); string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { if ((int)encodedBytes[i] == 63) count += 2; else count += 1; if (count <= maxlen - endStr.Length) outputStr += temp.Substring(i, 1); else if (count > maxlen) break; } if (count <= maxlen) { outputStr = temp; endStr = ""; } outputStr += endStr; return outputStr; }
以上就是C# 截取字符串方法(包含中英混合)的实现代码,需要的朋友可以参考一下
您可能感兴趣的文章:
相关文章推荐
- C#中英文混合字符串截取函数
- PHP截取含中文的混合字符串长度的函数
- PHP中英混合字符串截取的函数代码
- sql 中英文 混合 字符串截取函数
- VB截取中英文混合字符串中若干半角单位长字串的函数
- C#获取中英文混合字符串长度和截取函数
- c#中英文混合字符串截取指定长度
- sql 2005 中英文混合的字符串截取函数
- VB截取中英文混合字符串中若干半角单位长字串的函数
- PHP中英混合字符串截取函数代码
- PHP截取含中文的混合字符串长度的函数
- php中英混合字符串截取的函数代码
- php可包含混合字符串长度的截取函数
- C#中英文混合字符串截取指定长度
- PHP中英混合字符串截取函数代码
- C#判断中英文混合字符串长度及截取函数
- thinkPHP内置字符串截取函数用法详解
- Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数
- php中的字符串常用函数(二) substr() 截取字符串
- Mysql字符串截取函数SUBSTRING的用法说明(转)