C#截取字符串按字节截取SubString

 public static string CutByteString(string str,int len)
    {
      string result=string.Empty;// 最终返回的结果
      if(string.IsNullOrEmpty(str)) { return result; }
      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;
    }


 public static string CutByteString(string str,int startIndex,int len)

    {
      string result=string.Empty;// 最终返回的结果
      if(string.IsNullOrEmpty(str)) { return result; }
      int byteLen=System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度
      int charLen=str.Length;// 把字符平等对待时的字符串长度
      if(startIndex==0)
      { return CutByteString(str,len); }
      else if(startIndex>=byteLen)
      { return result; }
      else //startIndex < byteLen
      {
        int AllLen=startIndex+len;
        int byteCountStart=0;// 记录读取进度
        int byteCountEnd=0;// 记录读取进度
        int startpos=0;// 记录截取位置                
        int endpos=0;// 记录截取位置
        for(int i=0;i<charLen;i++)
        {
          if(Convert.ToInt32(str.ToCharArray()[i])>255)// 按中文字符计算加2
          { byteCountStart+=2; }
          else// 按英文字符计算加1
          { byteCountStart+=1; }
          if(byteCountStart>startIndex)// 超出时只记下上一个有效位置
          {
            startpos=i;
            AllLen=startIndex+len-1;
            break;
          }
          else if(byteCountStart==startIndex)// 记下当前位置
          {
            startpos=i+1;
            break;
          }
        }
        if(startIndex+len<=byteLen)//截取字符在总长以内
        {
          for(int i=0;i<charLen;i++)
          {
            if(Convert.ToInt32(str.ToCharArray()[i])>255)// 按中文字符计算加2
            { byteCountEnd+=2; }
            else// 按英文字符计算加1
            { byteCountEnd+=1; }
            if(byteCountEnd>AllLen)// 超出时只记下上一个有效位置
            {
              endpos=i;
              break;
            }
            else if(byteCountEnd==AllLen)// 记下当前位置
            {
              endpos=i+1;
              break;
            }
          }
          endpos=endpos-startpos;
        }
        else if(startIndex+len>byteLen)//截取字符超出总长
        {
          endpos=charLen-startpos;
        }
        if(endpos>=0)
        { result=str.Substring(startpos,endpos); }
      }
      return result;
    }

调用:
    private void button1_Click(object sender,EventArgs e)
    {
      string s="一二3456七八";
      s=CutByteString(s,5);

      MessageBox.Show(s);    //输出 “一二3”


      s=CutByteString(s,3,5);
      MessageBox.Show(s);     //输出 “二345”

    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值