C#中文字符截取函数

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///str_value 字符
ExpandedBlockEnd.gif
///str_len 要截取的字符长度

None.gif    public   string  leftx( string  str_value, int  str_len)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
int p_num = 0;   
InBlock.gif   
int i;
InBlock.gif   
string New_Str_value = "";
InBlock.gif
InBlock.gif   
if (str_value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    New_Str_value 
= "";
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   
int Len_Num = str_value.Length;
InBlock.gif
InBlock.gif   
InBlock.gif
InBlock.gif   
//if (Len_Num < str_len)
InBlock.gif   
//{
InBlock.gif   
// str_len = Len_Num;
InBlock.gif   
//}
InBlock.gif

InBlock.gif
InBlock.gif   
for (i = 0;i<=Len_Num - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
//str_value.Substring(i,1);
InBlock.gif
     if (i >Len_Num) break;
InBlock.gif     
char c  = Convert.ToChar(str_value.Substring(i,1));
InBlock.gif     
if  (((int)c > 255|| ((int)c<0))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      p_num 
= p_num + 2;
InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      p_num 
= p_num + 1;
InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
if (p_num >= str_len)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
InBlock.gif      New_Str_value 
= str_value.Substring(0,i+1);
InBlock.gif      
InBlock.gif      
break;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      New_Str_value 
= str_value;      
ExpandedSubBlockEnd.gif     }

InBlock.gif   
ExpandedSubBlockEnd.gif    }

InBlock.gif   
ExpandedSubBlockEnd.gif      }

InBlock.gif   
return New_Str_value;
ExpandedBlockEnd.gif  }

None.gif



C#截取中英文混合字符串

注意:需要引用正则名字空间,using System.Text.RegularExpressions;


调用方法:

CnCutString(ArtTitle, 24, "..");

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif  
/// 截取中英文混合字符串
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="original">原始字符串</param>
InBlock.gif  
/// <param name="length">截取长度</param>
InBlock.gif  
/// <param name="fill">截取串小于原始串时,尾部附加字符串</param>
ExpandedBlockEnd.gif  
/// <returns></returns>

None.gif    public   static  String CnCutString(String original, Int32 length, String fill)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   Regex CnRegex 
= new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
InBlock.gif   Char[] CharArray 
= original.ToCharArray();
InBlock.gif   Int32 tmplength 
= 0;
InBlock.gif   
for (Int32 i = 0; i < CharArray.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    tmplength 
+= CnRegex.IsMatch(CharArray[i].ToString()) ? 2 : 1;
InBlock.gif    
if (tmplength > length) return original.Substring(0, i - fill.Length) + fill;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
return original;
ExpandedBlockEnd.gif  }

None.gif

转载于:https://www.cnblogs.com/suchenge/articles/888742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值