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. X的追求道路

    X的追求道路 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 X在大家的帮助下最终找到了一个妹纸,于是開始了漫漫的追求之路,那 ...

  2. vue20 父子组件数据交互

    子组件使用父组件数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. Network Stack‎ : Disk Cache

    Disk Cache 目录 1 Overview 2 External Interface 3 Disk Structure 3.1 Cache Address 3.2 Index File Stru ...

  4. 原型,构造函数,实例,__proto__

    再说说__proto__,这个孩子性格慢向,所以即使在现代浏览器广为支持得今天也不建议使用,性能特别慢,而且影响所有来自该[[prototype]]的对象.只是拿出来了解了解: 1.它是个啥? 原型对 ...

  5. 入门Python:《趣学Python编程》中英文PDF+代码

    入门python推荐学习<趣学python编程>,语言轻松,通俗易懂,讲解由浅入深,力求将读者阅读和学习的难度降到最低.任何对计算机编程有兴趣的人或者首次接触编程的人,不论孩子还是成人,都 ...

  6. Python——Pygame实现生命游戏(game of life)

    模块:pygame import pygame,sys,time,random from pygame.locals import * """Color"&qu ...

  7. 实例讲解Nginx下的rewrite规则 来源:Linux社区

    一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...

  8. PHP 使用Apache 中的ab 測试站点的压力性能

    打开Apacheserver的安装路径(我用的是 WampServer),在bin文件夹中有一个ab.exe的可运行程序,它就是要介绍的压力測试工具. watermark/2/text/aHR0cDo ...

  9. iOS Core Animation具体解释(四)AutoLayout中的动画

    原创blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...

  10. webgoat7.1开发版环境搭建

    环境准备:git mavenjdktomcat然后进入正文:下载webgoat门户cd ~/webgoatgit clone https://github.com/WebGoat/WebGoat.gi ...