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

namespace ConsoleApplication2


{
class Program

{
static void Main(string[] args)

{
string strRule = "0123456789";
GB2312String gbstr = "你是A我是B";
GB2312String s2 = "我是B";
string str = gbstr;
Console.WriteLine("{0,17}", strRule);

Console.WriteLine("gbstr=\"
{0}\"" ,gbstr);

Console.WriteLine(" s2=\"
{0}\"", s2);
Console.WriteLine("----------------------------------------");
Console.WriteLine("gbstr.Substring(2,3) :{0}", gbstr.Substring(2, 3));
Console.WriteLine("gbstr.Substring(5) :{0}", gbstr.Substring(5));
Console.WriteLine("gbstr.IndexOf(\"我\") :{0}", gbstr.IndexOf("我"));
Console.WriteLine("gbstr.IndexOf(s2) :{0}", gbstr.IndexOf(s2));
Console.WriteLine("s2.IndexOf(gbstr) :{0}", s2.IndexOf(gbstr));
Console.WriteLine("gbstr.LastIndexOf(\"我是\") :{0}", gbstr.LastIndexOf("我是"));
Console.WriteLine("gbstr.Insert(4,\"C\")) :{0}", gbstr.Insert(4, "C"));
Console.WriteLine("gbstr.Remove(2,3) :{0}", gbstr.Remove(2, 3));

Console.WriteLine("gbstr.Split(new char[]{{'A'}})[1] :{0}", (gbstr.Split(new char[]
{ 'A' }))[1]);
Console.WriteLine("gbstr==\"你是A\"+\"我是B\" :{0}", gbstr == "你是A" + "我是B");
Console.WriteLine("gbstr.Equals(\"你是A我是B\") :{0}", gbstr.Equals("你是A我是B"));
Console.ReadKey();
}
}
}

输出结果
/*
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
*/
完整的类实现
using System;
using System.Collections.Generic;
using System.Text;

namespace MyTool


{
public class GB2312String

{
private string _str;
private string str

{

get
{ return _str; }
set

{
_str = value;
}
}

private byte[] _bytes;
private byte[] bytes

{
get

{
if (_bytes == null)
_bytes = coding.GetBytes(str);
return _bytes;
}
set

{
_bytes = value;
}
}
private static Encoding coding = Encoding.GetEncoding("gb2312");
public GB2312String(string str)

{
this.str = str;
}
public GB2312String(GB2312String o)
: this(o.ToString())

{
}
public GB2312String(byte [] bytes)

{
this.str = coding.GetString(bytes);
}
public GB2312String()
: this("")

{
}


public byte this[int index]

{

get
{ return bytes[index]; }
}

public int Length

{

get
{ return bytes.Length; }
}


public byte[] ToByteArray()

{
return bytes;
}


public int IndexOf(GB2312String dest)

{
int i = _str.IndexOf(dest);
if (i <= 0)
return i;
else
return coding.GetBytes(_str.Substring(0, i)).Length;
}

public GB2312String Substring(int startIndex, int length)

{
if ((startIndex < 0)
|| (length < 0)
|| (startIndex + length > bytes.Length))
throw new ArgumentOutOfRangeException();
else

{
byte[] substring = new byte[length];
Array.Copy(bytes, startIndex, substring,0, length);
return coding.GetString(substring);
}
}
public GB2312String Substring(int startIndex)

{
return Substring(startIndex, bytes.Length - startIndex);
}


/**//// <summary>
/// 返回对当前gb2312的引用
/// </summary>
/// <returns></returns>
public GB2312String Clone()

{
return this;
}

/**//// <summary>
/// 拷贝字符串到一字节数组中
/// </summary>
/// <param name="sourceIndex"></param>
/// <param name="destination"></param>
/// <param name="destinationIndex"></param>
/// <param name="count"></param>
/// <returns></returns>
public void CopyTo(int sourceIndex, byte[] destination, int destinationIndex, int count)

{
if (destination == null)
throw new ArgumentNullException();
else if ((sourceIndex + count > bytes.Length)
|| (destinationIndex + count > destination.Length)
|| (sourceIndex < 0) || (destinationIndex < 0) || (count < 0))
throw new ArgumentOutOfRangeException();
else

{
Array.Copy(bytes, sourceIndex, destination, destinationIndex, count);
}
}

/**//// <summary>
/// 是否包含子串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Contains(GB2312String value)

{
return str.Contains(value);
}

/**//// <summary>
/// 比较对象
/// </summary>
/// <param name="strB"></param>
/// <returns></returns>
public int CompareTo(GB2312String strB)

{
return str.CompareTo(strB);
}
public int CompareTo(object objB)

{
return str.CompareTo(objB);
}
public bool EndsWith(GB2312String value)

{
return str.EndsWith(value);
}


/**//// <summary>
/// 在startIndex 处插入子串
/// </summary>
/// <param name="startIndex"></param>
/// <param name="value"></param>
/// <returns></returns>
public GB2312String Insert(int startIndex, GB2312String value)

{
if (null == value)
throw new ArgumentNullException();
else if ((startIndex < 0) || (startIndex > bytes.Length))
throw new ArgumentOutOfRangeException();
else

{
byte[] newBytes = new byte[bytes.Length + value.Length];
Array.Copy(bytes, newBytes, startIndex);
Array.Copy(value.bytes, 0, newBytes, startIndex, value.Length);
Array.Copy(bytes, startIndex, newBytes, startIndex + value.Length, newBytes.Length - startIndex - value.Length);
return new GB2312String(newBytes);
}
}

public int LastIndexOf(GB2312String value, int startIndex)

{
int i = str.LastIndexOf(value, startIndex);
if (i == -1)
return i;
else
return coding.GetBytes(str.Substring(0, i)).Length;
}
public int LastIndexOf(GB2312String value)

{
return LastIndexOf(value, str.Length-1);
}

public GB2312String Remove(int startIndex,int count)

{
if ((startIndex < 0) || (count < 0)
|| (startIndex + count > bytes.Length))
throw new ArgumentOutOfRangeException();
else

{
byte [] newBytes = new byte[bytes.Length - count];
Array.Copy(bytes, 0, newBytes, 0, startIndex);
Array.Copy(bytes, startIndex + count, newBytes, startIndex, bytes.Length - startIndex - count);
return new GB2312String(newBytes);
}
}
public GB2312String Remove(int startIndex)

{
return Remove(startIndex, bytes.Length - startIndex);
}
public GB2312String Replace(GB2312String oldValue, GB2312String newValue)

{
return str.Replace(oldValue, newValue);
}
public GB2312String[] Split(char [] seperator)

{
string [] strList = str.Split(seperator);
GB2312String [] destList = new GB2312String[strList.Length];
for (int i = 0; i < strList.Length; i++)
destList[i] = strList[i];
return destList;
}

public bool StartsWith(GB2312String value)

{
return str.StartsWith(value);
}
public GB2312String ToLower()

{
return str.ToLower();
}
public GB2312String ToUpper()

{
return str.ToUpper();
}
public GB2312String Trim()

{
return str.Trim();
}
public GB2312String TrimStart()

{
return str.TrimStart();
}
public GB2312String TrimEnd()

{
return str.TrimEnd();
}




public override bool Equals(object obj)

{
if (obj == null)
return false;
else
return _str.Equals(obj.ToString());
}

public override string ToString()

{
return _str;
}

public override int GetHashCode()

{
return _str.GetHashCode();
}

public static bool operator ==(GB2312String o1, GB2312String o2)

{
if (object.Equals(o1, null) || object.Equals(o2, null))
return false;
else
return o1.Equals(o2);
}

public static bool operator !=(GB2312String o1, GB2312String o2)

{
return !(o1 == o2);
}

public static GB2312String operator +(GB2312String o1, GB2312String o2)

{
return new GB2312String(o1.ToString() + o2.ToString());
}

public static implicit operator string(GB2312String o)

{
return o.ToString();
}
public static implicit operator GB2312String(string str)

{
return new GB2312String(str);
}
}
}
