ITPub博客

首页 > 应用开发 > IT综合 > C#中一些字符串操作的常用用法

C#中一些字符串操作的常用用法

原创 IT综合 作者:BugRobber 时间:2007-06-21 08:38:30 0 删除 编辑
//获得汉字的区位码
  byte[] array = new byte[2];
  array = System.Text.Encoding.Default.GetBytes("啊");
  
  int i1 = (short)(array[0] - '');
  int i2 = (short)(array[1] - '');
  
  //unicode解码方式下的汉字码
  array = System.Text.Encoding.Unicode.GetBytes("啊");
  i1 = (short)(array[0] - '');
  i2 = (short)(array[1] - '');
  
  //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 );

http://www.linuxmine.com/48220.html

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-920001/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    372
  • 访问量
    200880