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. 关于App程序猿泡沫

    前言 做开发快七年了,对于程序猿,外行人总有着数不完的讽刺和误解,可是我都懒得去解释.代码搬运工人也好,民工也罢,随他们去说吧.可是网上近期流传的程序猿泡沫,尤其是APP程序猿泡沫的文章导致非常多我们 ...

  2. ios svn repository

    xcode默认自带Git和svn,首先讲下xcode4.6.3下配置svn: 1.检測你的mac中是否安装了svn: (1) 打开终端,输入 svn --version 假设出现下图信息,则说明已经安 ...

  3. DNS A记录和CNAME记录

    参考文章:http://blog.xieyc.com/differences-between-a-record-and-cname-record/ A (Address) 记录是用来指定主机名(或域名 ...

  4. 101.C++继承三种权限

    三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员函数访问 三种继承特点 1.public继承不改变基类成员的访问权限 ...

  5. Weka中数据挖掘与机器学习系列之Weka简介(二)

    不多说,直接上干货! Weka简介 Weka是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis)的英文字首缩写,官方网址为:http://www ...

  6. vSphere5安装配置视频教程

    vSphere5安装配置视频教程 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/350944/819550

  7. 消息推送学习一、原生Socket的使用

    消息推送也是客户端和服务器连接然后进行交互的一种形式,但是不同于HTTP的连接,这种连接需要长时间的进行,当有消息时可以及时推送到客户端.除此之外还有多个用户,可能需要针对其身份进行不同的推送等等要求 ...

  8. idea+maven+springmvc

    黑了Java这么多年, 今天为Java写一篇文章吧. 这篇文章主要是想帮助那些刚接触到Java, 同时想从事Java WEB GUI开发的人. 对我而言, 我很早就有想尝试用Java写WEB的想法, ...

  9. Ubuntu系统下的多路径软件 DM Multipath 配置。

    Ubuntu系统下的多路径软件是操作系统自带的 DM Multipath工具.------------------------------------------------------------- ...

  10. 如何在Ubuntu14.04中安装mysql

    接触过MySQL的小伙伴们都知道,在Windows下安装MySQL是一件让人十分头大的事情,但是在Ubuntu等其他Linux系统中安装MySQL就简单很多了,具体的教程如下.1.在Ubuntu的命令 ...