public class StringHelper     {         public static string GetSubString(string str, int len)         {             string result = string.Empty;// 最终返回的结果             int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度             int charLen = str.Length;// 把字符平等对待时的字符串长度             int byteCount = 0;// 记录读取进度             int pos = 0;// 记录截取位置             if (byteLen > len)             {                 for (int i = 0; i < charLen; i++)                 {                     if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2                         byteCount += 2;                     else// 按英文字符计算加1                         byteCount += 1;

if (byteCount > len)// 超出时只记下上一个有效位置                     {                         pos = i;                         break;                     }                     else if (byteCount == len)// 记下当前位置                     {                         pos = i + 1;                         break;                     }                 }

if (pos >= 0)                     result = str.Substring(0, pos);             }             else                 result = str;

return result;         }

/// <summary>         /// c#的中英文混合字符串截取         /// </summary>         /// <param name="inputString"></param>         /// <param name="length">显示的字符长度*2</param>         /// <returns></returns>         public static string SubString(string inputString, int length)         {             byte[] ping = Encoding.UTF8.GetBytes(inputString);             int count=Encoding.UTF8.GetByteCount(inputString);             if (count <= length * 2)             {                 return inputString;             }             ASCIIEncoding ascii = new ASCIIEncoding();             int tempLen = 0;             string tempString = "";             byte[] s = ascii.GetBytes(inputString);             for (int i = 0; i < s.Length; i++)             {                 ////判断是否为汉字或全脚符号                 if ((int)s[i] == 63)                 {                     tempLen += 2;                 }                 else                 {                     tempLen += 1;                 }                 tempString += inputString.Substring(i, 1);                 if (tempLen >= length * 2)                     break;             }             return tempString;         }

public static string GetSub(string sub, int length)         {             //byte[] bytStr = System.Text.Encoding.Default.GetBytes(sub);                if (sub == null) return string.Empty;             int len = length * 2;             //aequilateLength为中英文等宽长度,cutLength为要截取的字符串长度             int aequilateLength = 0, cutLength = 0;             Encoding encoding = Encoding.GetEncoding("gb2312");

string cutStr = sub.ToString();             int strLength = cutStr.Length;             byte[] bytes;             for (int i = 0; i < strLength; i++)             {                 bytes = encoding.GetBytes(cutStr.Substring(i, 1));                 if (bytes.Length == 2)//不是英文                     aequilateLength += 2;                 else                     aequilateLength++;

if (aequilateLength <= len) cutLength += 1;

if (aequilateLength > len)                     return cutStr.Substring(0, cutLength);//+ "..."             }             return cutStr;         }

}

c#的中英文混合字符串截取的更多相关文章

  1. c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)

    /// <summary>        /// c#的中英文混合字符串截取(区分中英文)        /// </summary>        /// <param ...

  2. c#的中英文混合字符串截取指定长度,startidx从0开始

    //c#的中英文混合字符串截取指定长度,startidx从0开始 by gisoracle@126.com public string getStrLenB(string str, int start ...

  3. 中英文混合字符串截取java

    //截取字符串长度(中文2个字节,半个中文显示一个) public String subTextString(String str,int len){ if(str.length()<len/2 ...

  4. PHP获取中英文混合字符串长度及截取

    1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 *  ...

  5. CSS截取中英文混合字符串长度

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  6. 用C#截取指定长度的中英文混合字符串

    很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则 ...

  7. C#与JS实现 获取指定字节长度 中英文混合字符串 的方法

    平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要 ...

  8. Lua截取utf-8编码的中英文混合字符串

    参考博客:UTF8字符串在lua的截取和字数统计[转载] 需求 按字面个数来截取子字符串 函数(字符串, 开始位置, 截取长度) utf8sub(,) = 好1世界哈 utf8sub(,) = 你好1 ...

  9. 用JS来实现于截取中英文混合字符串方法(转载)

    网站制作过程中,提示层文字超出,需要JS做字符串截取,但是呢,我们常常会烦恼文字中英文混合如何判断,因为我们知道在JS中 string.length这个值是不考虑中英文的,但是计算机对中英文的识别是  ...

随机推荐

  1. UITextView自己定义键盘和系统键盘

    UITextView有inputView 和 inputAccessoryView 两个属性,都指定了对应的视图. inputAccessoryView 对象显示在 inputView 对象的上面.与 ...

  2. sublime配置python

    Sublime Text 2作为一款轻量级的编辑器,特点鲜明.方便使用,愈发受到普罗大众的喜爱.我个人近期也開始用了起来.同一时候,我近段时间还在学习Python的相关东西.所以開始用ST2来写Pyt ...

  3. 会变得ActionBar,让你的ActionBar与众不同

    话不多说先看两张图: github地址:https://github.com/Smalinuxer/android-SlideActionBar 原理什么的有时间再讲,或者自行看代码; 兴许还会补充新 ...

  4. Linux系统的LOG日志文件及入侵后日志的清除

    UNIX网管员主要是靠系统的LOG,来获得入侵的痕迹.当然也有第三方工具记录入侵系统的 痕迹,UNIX系统存放LOG文件,普通位置如下: /usr/adm - 早期版本的UNIX/var/adm -  ...

  5. 1.Apache Axis配置文件WSDD详解

    转自:https://jyao.iteye.com/blog/1285516 1. Aapche Axis的Web Service Deployment Descriptor(WSDD)文件参考文档. ...

  6. OSSIM(开源安全信息管理系统)在企业网络管理中的应用

    国内首个Ossim技术交流群(179084574),欢迎加入我们 参与51CTO[第242期]OSSIM,企业信息安全管理利器热门技术讨论 650) this.width=650;" bor ...

  7. 机器学习(三) Jupyter Notebook, numpy和matplotlib的详细使用 (下)

    七.Numpy中的矩阵运算 八.Numpy中的聚合运算 九.Numpy中的arg运算 十.Numpy中的比较和Fancy Indexing 十一.Matplotlib数据可视化基础 十二.数据加载和简 ...

  8. Java读取txt文件和覆盖写入txt文件和追加写入txt

    //创建文件 public static void createFile(File filename) { try { if(!filename.exists()) { filename.create ...

  9. js数组中foEach和map的用法详解 jq中的$.each和$.map

    数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...

  10. Laravel 框架指定路由关闭 csrf

    修改 app\Http\Middleware\VerifyCsrfToken.php 内容: <?php namespace App\Http\Middleware; use Closure; ...