/// <summary>        /// c#的中英文混合字符串截取(区分中英文)        /// </summary>        /// <param name="inputString"></param>        /// <param name="byteLength">要输出的字节长度</param>        /// <returns></r…
public class StringHelper     {         public static string GetSubString(string str, int len)         {             string result = string.Empty;// 最终返回的结果             int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度          …
//c#的中英文混合字符串截取指定长度,startidx从0开始 by gisoracle@126.com public string getStrLenB(string str, int startidx, int len) { int Lengthb = getLengthb(str); if (startidx + 1 > Lengthb) { return ""; } int j = 0; int l = 0; int strw = 0;//字符的宽度 bool b = …
//截取字符串长度(中文2个字节,半个中文显示一个) public String subTextString(String str,int len){ if(str.length()<len/2)return str; int count = 0; StringBuffer sb = new StringBuffer(); String[] ss = str.split(""); for(int i=1;i<ss.length;i++){ count+=ss[i].getB…
很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则Match的速度,比如文章系统中就极有可能用到,今天有点时间,就改进了一下,代码如下: public static string getStr(string s,int l,string endStr) { , (s.Length < l)?s.Length:l); if (Regex.Replac…
1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 * @param $$charset string 编码 * @return 返回长度,1中文=1位,2英文=1位 */ function strLength($str,$charset='utf-8'){ if($charset=='utf-8') $str = iconv('utf-8','gb23…
题目: 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个". GB2312.GBK.GB18030,CP936以及CNS11643都满足条件 -- 中文是占用2个字节的,英文是占用1一个字节 . 因为中文转换为byte字节,随着编码的不…
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CSS截取中英文混合字符串长度</title> <meta name="keywords" content="" /> <me…
平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要占两个字节,所以对混合型的字符串就要作一个处理,统一按字节长度来计算字符串长度,方法如下: C#方法一: public static string GetString(string str, int len) { string result = string.Empty;// 最终返回的结果 int…
前言 因为之前java课设做的是股票分析系统,我找的接口返回的是一个.csv文件,因为这种文件里面的数据是以逗号分隔的,所以要对数据进行分析的时候需要截取子串,并且以逗号作为截取的标志.所以接下来就说一下我使用的字符串函数 substring和indexOf. -----------------------------------------------------------------------------------------------------------------------…