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这个值是不考虑中英文的,但是计算机对中英文的识别是 ...
随机推荐
- 进程:linux用户态-内核态
用户态:Ring3运行于用户态的代码则要受到处理器的诸多检查,它们只能访问映射其地址空间的页表项中规定的在用户态下可访问页面的虚拟地址,且只能对任务状态段(TSS)中I/O许可位图(I/O Permi ...
- [React] Make Controlled React Components with Control Props
Sometimes users of your component want to have more control over what the internal state is. In this ...
- C#读写共享目录
C#读写共享目录 该试验分下面步骤: 1.在server设置一个共享目录.在这里我的serverip地址是10.80.88.180,共享目录名字是test,test里面有两个文件:good.txt和b ...
- HDU 5371 Hotaru's problem Manacher+尺取法
题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半. 思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个 ...
- java 文件读写demo
分析错误日志: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public ...
- Android 使用Gallery组件实现图片播放预览
Gallery(画廊)扩展了LayoutParams,以此提供可以容纳当前的转换信息和先前的位置转换信息的场所. Activity package com.app.test01; import com ...
- ThinkPad X260 UEFI安装 win7 64位 方法
ThinkPad X260 UEFI安装 win7 64位 方法 1.使用DG重新格式化硬盘,格式为GPT 2.使用CGI 安装 WIM文件 (image不知是否可以,下次测试) 3.改BIOS ...
- mkdi---创建目录。
mkdir命令用来创建目录.该命令创建由dirname命名的目录.如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录:如果给出了一个已经存在的路径,将会在该目录下创建一个 ...
- 学习——HTML5
HTML5多用于手机页面制作,因为PC版浏览器大多不兼容,可以通过下面网站查看HTML5浏览器兼容情况: http://www.caniuse.com/#index 一.语义化标签 1.<hea ...
- android側滑菜单-DrawerLayout的基本使用
眼下主流App开发中,部分是以側滑菜单为主布局架构,曾经做android側滑菜单时.大多选择使用github上的第三方开源框架SildingMenu,可是这个框架还是稍显笨重.好消息是google已经 ...