c# stream byte char string 流、位元組、字元、字串

來源:互聯網
上載者:User

1、兩種不同的方法計算字串的長度

        string strTmp = "wk986王克東";
           
        int i = System.Text.Encoding.Default.GetBytes(strTmp).Length;    //算漢字的長度
        int j = strTmp.Length;                                                                     //不算漢字的長度

        Console.WriteLine("字串{0},算漢字的長度:{1},不算漢字長度:{2}",     strTmp,i,j);

        //轉換成數組計算數組的長度

        byte[] bytStr = System.Text.Encoding.Default.GetBytes(strTmp);   
        int len = bytStr.Length;
        Console.WriteLine("字串長度:"+len.ToString());
        Console.Read();

 

2、System.Text.StringBuilder("")

       和字串“+”是不一樣的,在C#中,字串是“引用”類型,每加一個是重建立立了一個字串,當字串特別大的時候,效能消耗大,所以要用StringBuilder。

         System.Text.StringBuilder sb = new System.Text.StringBuilder(""); 
         sb.Append("中華"); 
         sb.Append("人民"); 
         sb.Append("共和國");
         Console.WriteLine(sb);

//判斷漢字個數

private int ChkGBKLen(string str)
{
System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
    byte[] b = n.GetBytes(str);
    int l = 0;
    for (int i = 0; i <= b.Length - 1; i++)
    {
     if (b[i] == 63) //判斷是否為漢字或全腳符號
        {
            l++;
        }
    }
    return l;
}

C#中流,位元組,字元,字串2010-04-19 23:01

首先要明白它們本身是由什麼組成的:

流:二進位

位元組:不帶正負號的整數

字元:Unicode編碼字元

字串:多個Unicode編碼字元

那麼在.net下它們之間如何轉化呢?

一般是遵守以下規則:

流->位元組數組->字元數組->字串

下面就來具體談談轉化的文法:

流->位元組數組

MemoryStream ms = new MemoryStream();

byte[] buffer = new byte[ms.Length];

ms.Read(buffer, 0, (int)ms.Length);

位元組數組->流

byte[] buffer = new byte[10];

MemoryStream ms = new MemoryStream(buffer);

位元組數組->字元數組

1.

byte[] buffer = new byte[10];

char[] ch = new ASCIIEncoding().GetChars(buffer);

//或者:char[] ch = Encoding.UTF8.GetChars(buffer)

2.

byte[] buffer = new byte[10];

char[] ch = new char[10];

for(int i=0; i<buffer.Length; i++)

{

    ch[i] = Convert.ToChar(buffer[i]);

}

字元數組->位元組數組

1.

char[] ch = new char[10];

byte[] buffer = new ASCIIEncoding().GetBytes(ch);

//或者:byte[] buffer = Encoding.UTF8.GetBytes(ch)

2.

char[] ch = new char[10];

byte[] buffer = new byte[10];

for(int i=0; i<ch.Length; i++)

{

    buffer[i] = Convert.ToByte(ch[i]);

}

字元數組->字串

char[] ch = new char[10];

string str = new string(ch);

字串->字元數組

string str = "abcde";

char[] ch=str .ToCharArray();

位元組數組->字串

byte[] buffer = new byte[10];

string str = System.Text.Encoding.UTF8.GetString(buffer);

//或者:string str = new ASCIIEncoding().GetString(buffer);

字串->位元組數組

string str = "abcde";

byte[] buffer=System.Text.Encoding.UTF8.GetBytes(str);

//或者:byte[] buffer= new ASCIIEncoding().GetBytes(str);

說明:主要就是用到了Convert類和System.Text命名空間下的類,Encoding是靜態類,ASCIIEncoding是實體類,方法都是一樣的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.