由于工作关系,需要使用到一些老系统,在这些老系统中,都是使用一个中文汉字2个字节,英文字符1个字节来表示的。
  但是在.Net框架中,内部的字符串表示都是采用Unicode表示,这在字符串截取,计算长度等方面带来了一系列问题,本文提供了一个GB2312String类,用来解决此类问题。
  在VS2005下编译成功。本例仅作抛砖引玉。


调用代码
GB2312 字符串,单字节英文,双字节中文的完整类实现using System;
GB2312 字符串,单字节英文,双字节中文的完整类实现
using System.Collections.Generic;
GB2312 字符串,单字节英文,双字节中文的完整类实现
using System.Text;
GB2312 字符串,单字节英文,双字节中文的完整类实现
using MyTool;
GB2312 字符串,单字节英文,双字节中文的完整类实现
GB2312 字符串,单字节英文,双字节中文的完整类实现
namespace ConsoleApplication2

输出结果

/*
       0123456789
gbstr="你是A我是B"
   s2="我是B"
----------------------------------------
gbstr.Substring(2,3)            :是A
gbstr.Substring(5)              :我是B
gbstr.IndexOf("我")             :5
gbstr.IndexOf(s2)               :5
s2.IndexOf(gbstr)               :-1
gbstr.LastIndexOf("我是")       :5
gbstr.Insert(4,"C"))            :你是CA我是B
gbstr.Remove(2,3)               :你我是B
gbstr.Split(new char[]{'A'})[1] :我是B
gbstr=="你是A"+"我是B"          :True
gbstr.Equals("你是A我是B")      :True
*/


完整的类实现

 

 

GB2312 字符串,单字节英文,双字节中文的完整类实现using System;
GB2312 字符串,单字节英文,双字节中文的完整类实现
using System.Collections.Generic;
GB2312 字符串,单字节英文,双字节中文的完整类实现
using System.Text;
GB2312 字符串,单字节英文,双字节中文的完整类实现
GB2312 字符串,单字节英文,双字节中文的完整类实现
namespace MyTool


 

相关文章:

  • 2021-10-08
  • 2021-07-30
  • 2021-11-29
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-12-25
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案