/// <param name="str"> 被截取的字符串 </param> /// <param name="len"> 所截取的长度 </param> /// <returns> 子字符串 </returns> public static string CutString( string str, int len) if (str == null || str.Length == 0 || len <= 0 ) return string .Empty; int l = str.Length; #region 计算长度 int clen = 0 ; while (clen < len && clen < l) // 每遇到一个中文,则将目标长度减一。 if (( int )str[clen] > 128 ) { len-- ; } clen ++ ; #endregion if (clen < l) return str.Substring( 0 , clen) + " ... " ; return str; /// <summary> /// //截取字符串中文 字母 /// </summary> /// <param name="content"> 源字符串 </param> /// <param name="length"> 截取长度! </param> /// <returns></returns> public static string SubTrueString( object content, int length) string strContent = NoHTML(content.ToString()); bool isConvert = false ; int splitLength = 0 ; int currLength = 0 ; int code = 0 ; int chfrom = Convert.ToInt32( " 4e00 " , 16 ); // 范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32( " 9fff " , 16 ); for ( int i = 0 ; i < strContent.Length; i++ ) code = Char.ConvertToUtf32(strContent, i); if (code >= chfrom && code <= chend) currLength += 2 ; // 中文 currLength += 1 ; // 非中文 splitLength = i + 1 ; if (currLength >= length) isConvert = true ; break ; if (isConvert) return strContent.Substring( 0 , splitLength); return strContent; public static int GetStringLenth( object content) string strContent = NoHTML(content.ToString()); int currLength = 0 ; int code = 0 ; int chfrom = Convert.ToInt32( " 4e00 " , 16 ); // 范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32( " 9fff " , 16 ); for ( int i = 0 ; i < strContent.Length; i++ ) code = Char.ConvertToUtf32(strContent, i); if (code >= chfrom && code <= chend) currLength += 2 ; // 中文 currLength += 1 ; // 非中文 return currLength; #endregion

以上便是完整代码,谢谢!

在此,顺便说下数据库按照汉字首字母进行排序的方法:

oracle :

在oracle9i中新增了按照拼音、部首、笔画排序功能。设置NLS_SORT值
SCHINESE_RADICAL_M 按照部首(第一顺序)、笔划(第二顺序)排序
SCHINESE_STROKE_M 按照笔划(第一顺序)、部首(第二顺序)排序
SCHINESE_PINYIN_M 按照拼音排序,系统的默认排序方式为拼音排序

举例如下:
表名为 dept ,其中name字段是中文,下面分别实现按照单位名称的笔划、部首和拼音排序。
//按照笔划排序
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_STROKE_M');
//按照部首排序
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_RADICAL_M');
//按照拼音排序,此为系统的默认排序方式
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_PINYIN_M');

sqlserver

select * from table order by name collate Chinese_PRC_CS_AS_KS_WS 

执行结果:select * from Table where cateFid=0 order by cateName collate Chinese_PRC_CS_AS_KS_WS 
如果您觉得不错,顶下吧!
2019年3月7日完善如下:
如何获取全拼呢? 引用第三方DLL。可以在NuGet库中引用:NPinyin
获取全拼的代码!
  public static string GetSpellAllCode(string CnStr)
           return NPinyin.Pinyin.GetPinyin(CnStr);

上述代码获取的全拼有空格分隔,如果你不需要空格,可采用:.Replace(" ", "")  把空格替换掉!

但是有个问题需要完善下:

如果上述获取首字母的方法得到的结果中包含‘?’号,这就说明上述方法存在不能翻译的汉字,这时候我们可以将二者相结合,如下:

  string szm = PingYinHelper.GetSpellCode(smSupplierModel.SupplierName);
                if (szm.Contains("?"))
                    szm = string.Empty;
                    var ary = PingYinHelper.GetSpellAllCode(smSupplierModel.SupplierName).Split(' ');
                    foreach (var py in ary)
                        szm += py.Substring(0, 1);
                string supplierSZM = szm.ToUpper() + "_" + PingYinHelper.GetSpellAllCode(smSupplierModel.SupplierName).Replace(" ", "");
                smSupplierEntity.Relatcpyno = supplierSZM;

@陈卧龙的博客