C#字符串操作步驟
作者:佚名 
  這里介紹C#字符串操作步驟,通過一個一個實例的介紹,執(zhí)行并顯示一些結果,從而說明C#字符串操作步驟。
 在C#字符串中,System.String類是一個引用類型,但與其他引用類型不同的是,將C#字符串視為一個基本類型:可以聲明為一個常量,并可以直接賦值。這里將介紹操作步驟。
操作步驟代碼:
- //獲得漢字的區(qū)位碼
 - byte[] array = new byte[2];
 - array = System.Text.Encoding.Default.GetBytes("啊");
 - int i1 = (short)(array[0] - ''\0'');
 - int i2 = (short)(array[1] - ''\0'');
 - //unicode解碼方式下的漢字碼
 - array = System.Text.Encoding.Unicode.GetBytes("啊");
 - i1 = (short)(array[0] - ''\0'');
 - i2 = (short)(array[1] - ''\0'');
 - //unicode反解碼為漢字
 - string str = "4a55";
 - string s1 = str.Substring(0,2);
 - string s2 = str.Substring(2,2);
 - int t1 = Convert.ToInt32(s1,16);
 - int t2 = Convert.ToInt32(s2,16);
 - array[0] = (byte)t1;
 - array[1] = (byte)t2;
 - string s = System.Text.Encoding.Unicode.GetString(array);
 - //default方式反解碼為漢字
 - array[0] = (byte)196;
 - array[1] = (byte)207;
 - s = System.Text.Encoding.Default.GetString(array);
 - //取字符串長度
 - s = "iam方槍槍";
 - int len = s.Length;//will output as 6
 - byte[] sarr = System.Text.Encoding.Default.GetBytes(s);
 - len = sarr.Length;//will output as 3+3*2=9
 - //字符串相加
 - System.Text.StringBuilder sb = new System.Text.StringBuilder("");
 - sb.Append("i ");
 - sb.Append("am ");
 - sb.Append("方槍槍");
 - string --> byte array
 - byte[] data=Syste.Text.Encoding.ASCII.GetBytes(string);
 - string --> byte
 - byte data = Convert.ToByte(string);
 - byte[]-->string
 - string string = Encoding.ASCII.GetString( bytes, 0, nBytesSize );
 
以上介紹C#字符串操作步驟
【編輯推薦】
責任編輯:佚名 
                    來源:
                    IT168
 














 
 
 
 
 
 
 