c#的中英文混合字符串截取
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#的中英文混合字符串截取的更多相关文章
- c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)
/// <summary> /// c#的中英文混合字符串截取(区分中英文) /// </summary> /// <param ...
- c#的中英文混合字符串截取指定长度,startidx从0开始
//c#的中英文混合字符串截取指定长度,startidx从0开始 by gisoracle@126.com public string getStrLenB(string str, int start ...
- 中英文混合字符串截取java
//截取字符串长度(中文2个字节,半个中文显示一个) public String subTextString(String str,int len){ if(str.length()<len/2 ...
- PHP获取中英文混合字符串长度及截取
1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 * ...
- CSS截取中英文混合字符串长度
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- 用C#截取指定长度的中英文混合字符串
很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则 ...
- C#与JS实现 获取指定字节长度 中英文混合字符串 的方法
平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要 ...
- Lua截取utf-8编码的中英文混合字符串
参考博客:UTF8字符串在lua的截取和字数统计[转载] 需求 按字面个数来截取子字符串 函数(字符串, 开始位置, 截取长度) utf8sub(,) = 好1世界哈 utf8sub(,) = 你好1 ...
- 用JS来实现于截取中英文混合字符串方法(转载)
网站制作过程中,提示层文字超出,需要JS做字符串截取,但是呢,我们常常会烦恼文字中英文混合如何判断,因为我们知道在JS中 string.length这个值是不考虑中英文的,但是计算机对中英文的识别是 ...
随机推荐
- C++模板中重要的术语
- WEB前端,混合排版,有的宽有的窄,滚动会出现空白处,怎么办。
多数时候出现空白都是由于有滚动栏滚到一边就会产生空白. overflow-x: hidden; 在最大图的那个div里写这句.
- ruby redis的集群管理器
#========================================================================================== # => ...
- BZOJ 3262 cdq分治 OR 树套树
注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...
- HDU 4372 Count the Buildings
Count the Buildings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- C# Cache的类方法
public class DataCache { /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 / ...
- TwinCAT 3中基于UDP协议通讯的C++实现
因为项目需要,学习了TwinCAT3中使用UDP协议进行通讯的基本知识.这个做个简单的笔记,方便以后查询. 1 概述 倍福为了实现从实时环境中直接访问网卡(network cards)专门提供了一个函 ...
- JAVA数组的基本方法
数组的基本方法 数组可以存放多个数据,多个数据类型要统一数组格式: 格式一:常用写法 数组类型[] 数组名称 = new 数据类型[数组长度]; 格式二:蛋疼写法 数组类型[] 数组名称; 数组名称 ...
- qt程序实现打开文件夹
QString path=QDir::currentPath();//获取程序当前目录 path.replace("/","\\");//将地址中的" ...
- 为线程绑定CPU
// learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...