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这个值是不考虑中英文的,但是计算机对中英文的识别是 ...
随机推荐
- ArcGIS api for javascript——渲染-计算相等间隔分级
描述 本例展示了如何配置分级渲染使用一个相等间隔分类.在这个分类类型中,断点被设置为相等的距离. 可以手工添加相等距离的断点:然而,如果数据被修改了,那些断点就会是不合理的.本例自动地计算断点,因此相 ...
- hdu 1575 矩阵连乘2
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- uvalive 4730王国kingdom(并查集+线段树)
题意:有T组測试数据.每组数据的N表示有N个城市,接下来的N行里每行给出每一个城市的坐标(0<=x,y<=1000000),然后有M(1<M<200000)个操作,操作有 ...
- Qt中事件分发源码剖析
Qt中事件分发源码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,而且进行分发,这些都是在exec中进行的. 以下举例说明: 1)首先看看以下一段演示样例代码: ...
- 面试基础_03实现strcpy、strcat、strcmp、strlen
实现代码例如以下: /************************************************************************* > File Name: ...
- MVP模式入门(结合Rxjava,Retrofit)
本文MVP的sample实现效果: github地址:https://github.com/xurui1995/MvpSample 老规矩,在说对MVP模式的理解之前还是要再谈谈MVC模式,了解了MV ...
- vSphere5安装配置视频教程
vSphere5安装配置视频教程 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/350944/819550
- 应用层协议——HTTP
HTTP: Client HTTP <--> 应用程序HTTP报文 <--> Server HTTP 应用程序 由TCP支持 Statelessprotocol:不会记录客户端 ...
- php验证邮箱,手机号是否正确
function is_valid_email($email)//判断是不是邮箱的函数{ return preg_match('/^[a-zA-Z0-9._%-]+@([a-zA-Z0-9.-] ...
- Dcloud+mui 压缩上传图片到服务器
chooseImgFromAlbums选择图片 chooseImgFromPictures 拍照 changeToLocalUrl 转换成可用的路径 uploadpic.compressImg 压缩图 ...