C#截取字串按位元組截取SubString

來源:互聯網
上載者:User

標籤:ar   os   sp   for   on   art   bs   ef   tt   

 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”

    }


C#截取字串按位元組截取SubString

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.