由于工作关系,需要使用到一些老系统,在这些老系统中,都是使用一个中文汉字2个字节,英文字符1个字节来表示的。
但是在.Net框架中,内部的字符串表示都是采用Unicode表示,这在字符串截取,计算长度等方面带来了一系列问题,本文提供了一个GB2312String类,用来解决此类问题。
在VS2005下编译成功。本例仅作抛砖引玉。
调用代码
using
System;
using
System.Collections.Generic;
using
System.Text;
using
MyTool;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ConsoleApplication2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
class Program
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strRule = "0123456789";
GB2312String gbstr = "你是A我是B";
GB2312String s2 = "我是B";
string str = gbstr;
Console.WriteLine("{0,17}", strRule);
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Console.WriteLine("gbstr=\"
{0}\"" ,gbstr);
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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));
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
MyTool
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class GB2312String
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _str;
private string str
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _str; }
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_str = value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private byte[] _bytes;
private byte[] bytes
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_bytes == null)
_bytes = coding.GetBytes(str);
return _bytes;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_bytes = value;
}
}
private static Encoding coding = Encoding.GetEncoding("gb2312");
public GB2312String(string str)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.str = str;
}
public GB2312String(GB2312String o)
: this(o.ToString())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public GB2312String(byte [] bytes)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.str = coding.GetString(bytes);
}
public GB2312String()
: this("")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public byte this[int index]
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return bytes[index]; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Length
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return bytes.Length; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public byte[] ToByteArray()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return bytes;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int IndexOf(GB2312String dest)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = _str.IndexOf(dest);
if (i <= 0)
return i;
else
return coding.GetBytes(_str.Substring(0, i)).Length;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public GB2312String Substring(int startIndex, int length)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((startIndex < 0)
|| (length < 0)
|| (startIndex + length > bytes.Length))
throw new ArgumentOutOfRangeException();
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] substring = new byte[length];
Array.Copy(bytes, startIndex, substring,0, length);
return coding.GetString(substring);
}
}
public GB2312String Substring(int startIndex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Substring(startIndex, bytes.Length - startIndex);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回对当前gb2312的引用
/// </summary>
/// <returns></returns>
public GB2312String Clone()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(bytes, sourceIndex, destination, destinationIndex, count);
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 是否包含子串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Contains(GB2312String value)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Contains(value);
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 比较对象
/// </summary>
/// <param name="strB"></param>
/// <returns></returns>
public int CompareTo(GB2312String strB)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.CompareTo(strB);
}
public int CompareTo(object objB)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.CompareTo(objB);
}
public bool EndsWith(GB2312String value)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.EndsWith(value);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 在startIndex 处插入子串
/// </summary>
/// <param name="startIndex"></param>
/// <param name="value"></param>
/// <returns></returns>
public GB2312String Insert(int startIndex, GB2312String value)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (null == value)
throw new ArgumentNullException();
else if ((startIndex < 0) || (startIndex > bytes.Length))
throw new ArgumentOutOfRangeException();
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int LastIndexOf(GB2312String value, int startIndex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return LastIndexOf(value, str.Length-1);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public GB2312String Remove(int startIndex,int count)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((startIndex < 0) || (count < 0)
|| (startIndex + count > bytes.Length))
throw new ArgumentOutOfRangeException();
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Remove(startIndex, bytes.Length - startIndex);
}
public GB2312String Replace(GB2312String oldValue, GB2312String newValue)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Replace(oldValue, newValue);
}
public GB2312String[] Split(char [] seperator)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool StartsWith(GB2312String value)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.StartsWith(value);
}
public GB2312String ToLower()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.ToLower();
}
public GB2312String ToUpper()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.ToUpper();
}
public GB2312String Trim()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Trim();
}
public GB2312String TrimStart()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.TrimStart();
}
public GB2312String TrimEnd()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.TrimEnd();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool Equals(object obj)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (obj == null)
return false;
else
return _str.Equals(obj.ToString());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _str;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override int GetHashCode()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _str.GetHashCode();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool operator ==(GB2312String o1, GB2312String o2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (object.Equals(o1, null) || object.Equals(o2, null))
return false;
else
return o1.Equals(o2);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool operator !=(GB2312String o1, GB2312String o2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return !(o1 == o2);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static GB2312String operator +(GB2312String o1, GB2312String o2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new GB2312String(o1.ToString() + o2.ToString());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static implicit operator string(GB2312String o)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return o.ToString();
}
public static implicit operator GB2312String(string str)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new GB2312String(str);
}
}
}