GB2312 字符串,单字节英文,双字节中文的完整类实现

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


调用代码
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  MyTool;
None.gif
None.gif
namespace  ConsoleApplication2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strRule     = "0123456789";
InBlock.gif            GB2312String gbstr 
= "你是A我是B";
InBlock.gif            GB2312String s2 
= "我是B";
InBlock.gif            
string str = gbstr;
InBlock.gif            Console.WriteLine(
"{0,17}", strRule);
ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(
"gbstr=\"dot.gif{0}\"" ,gbstr);
ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(
"   s2=\"dot.gif{0}\"", s2);
InBlock.gif            Console.WriteLine(
"----------------------------------------");
InBlock.gif            Console.WriteLine(
"gbstr.Substring(2,3)            :{0}", gbstr.Substring(23));
InBlock.gif            Console.WriteLine(
"gbstr.Substring(5)              :{0}", gbstr.Substring(5));
InBlock.gif            Console.WriteLine(
"gbstr.IndexOf(\"我\")             :{0}", gbstr.IndexOf(""));
InBlock.gif            Console.WriteLine(
"gbstr.IndexOf(s2)               :{0}", gbstr.IndexOf(s2));
InBlock.gif            Console.WriteLine(
"s2.IndexOf(gbstr)               :{0}", s2.IndexOf(gbstr));
InBlock.gif            Console.WriteLine(
"gbstr.LastIndexOf(\"我是\")       :{0}", gbstr.LastIndexOf("我是"));
InBlock.gif            Console.WriteLine(
"gbstr.Insert(4,\"C\"))            :{0}", gbstr.Insert(4"C"));
InBlock.gif            Console.WriteLine(
"gbstr.Remove(2,3)               :{0}", gbstr.Remove(23));
ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(
"gbstr.Split(new char[]{{'A'}})[1] :{0}", (gbstr.Split(new char[] dot.gif'A' }))[1]);
InBlock.gif            Console.WriteLine(
"gbstr==\"你是A\"+\"我是B\"          :{0}", gbstr == "你是A" + "我是B");
InBlock.gif            Console.WriteLine(
"gbstr.Equals(\"你是A我是B\")      :{0}", gbstr.Equals("你是A我是B"));
InBlock.gif            Console.ReadKey();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

输出结果

/*
       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
*/


完整的类实现

 

 

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  MyTool
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class GB2312String 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string _str;
InBlock.gif        
private string str
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _str; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _str 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private byte[] _bytes;
InBlock.gif        
private byte[] bytes
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_bytes == null)
InBlock.gif                    _bytes 
= coding.GetBytes(str);
InBlock.gif                
return _bytes;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _bytes 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private static Encoding coding = Encoding.GetEncoding("gb2312");
InBlock.gif        
public GB2312String(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.str = str;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String(GB2312String o)
InBlock.gif            : 
this(o.ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String(byte [] bytes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.str = coding.GetString(bytes);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String()
InBlock.gif            : 
this("")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public byte this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn bytes[index]; }
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif
InBlock.gif        
public int Length
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn bytes.Length; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public byte[] ToByteArray()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return bytes;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public int IndexOf(GB2312String dest)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i = _str.IndexOf(dest);
InBlock.gif            
if (i <= 0)
InBlock.gif                
return i;
InBlock.gif            
else
InBlock.gif                
return coding.GetBytes(_str.Substring(0, i)).Length;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public GB2312String Substring(int startIndex, int length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((startIndex < 0)
InBlock.gif                
|| (length < 0)
InBlock.gif                
|| (startIndex + length > bytes.Length))
InBlock.gif                
throw new ArgumentOutOfRangeException();
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] substring = new byte[length];
InBlock.gif                Array.Copy(bytes, startIndex, substring,
0, length);
InBlock.gif                
return coding.GetString(substring);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String Substring(int startIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Substring(startIndex, bytes.Length - startIndex);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回对当前gb2312的引用
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public GB2312String Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 拷贝字符串到一字节数组中
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sourceIndex"></param>
InBlock.gif        
/// <param name="destination"></param>
InBlock.gif        
/// <param name="destinationIndex"></param>
InBlock.gif        
/// <param name="count"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public void CopyTo(int sourceIndex, byte[] destination, int destinationIndex, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (destination == null)
InBlock.gif                
throw new ArgumentNullException();
InBlock.gif            
else if ((sourceIndex + count > bytes.Length)
InBlock.gif                
|| (destinationIndex + count > destination.Length)
InBlock.gif                
|| (sourceIndex < 0|| (destinationIndex < 0|| (count < 0))
InBlock.gif                
throw new ArgumentOutOfRangeException();
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Array.Copy(bytes, sourceIndex, destination, destinationIndex, count);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 是否包含子串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool Contains(GB2312String value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.Contains(value);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 比较对象
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strB"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int CompareTo(GB2312String strB)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.CompareTo(strB);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int CompareTo(object objB)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.CompareTo(objB);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public bool EndsWith(GB2312String value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.EndsWith(value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在startIndex 处插入子串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="startIndex"></param>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public GB2312String Insert(int startIndex, GB2312String value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (null == value)
InBlock.gif                
throw new ArgumentNullException();
InBlock.gif            
else if ((startIndex < 0|| (startIndex > bytes.Length))
InBlock.gif                
throw new ArgumentOutOfRangeException();
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] newBytes = new byte[bytes.Length + value.Length];
InBlock.gif                Array.Copy(bytes, newBytes, startIndex);
InBlock.gif                Array.Copy(value.bytes, 
0, newBytes, startIndex, value.Length);
InBlock.gif                Array.Copy(bytes, startIndex, newBytes, startIndex 
+ value.Length, newBytes.Length - startIndex - value.Length);
InBlock.gif                
return new GB2312String(newBytes);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        
public int LastIndexOf(GB2312String value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return LastIndexOf(value, str.Length-1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public GB2312String Remove(int startIndex,int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((startIndex < 0|| (count < 0)
InBlock.gif                
|| (startIndex + count > bytes.Length))
InBlock.gif                
throw new ArgumentOutOfRangeException();
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte [] newBytes = new byte[bytes.Length - count];
InBlock.gif                Array.Copy(bytes, 
0, newBytes, 0, startIndex);
InBlock.gif                Array.Copy(bytes, startIndex 
+ count, newBytes, startIndex, bytes.Length - startIndex - count);
InBlock.gif                
return new GB2312String(newBytes);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String Remove(int startIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Remove(startIndex, bytes.Length - startIndex);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String Replace(GB2312String oldValue, GB2312String newValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.Replace(oldValue, newValue);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String[] Split(char [] seperator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string [] strList = str.Split(seperator);
InBlock.gif            GB2312String [] destList 
= new GB2312String[strList.Length];
InBlock.gif            
for (int i = 0; i < strList.Length; i++)
InBlock.gif                destList[i] 
= strList[i];
InBlock.gif            
return destList;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool StartsWith(GB2312String value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.StartsWith(value); 
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String ToLower()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.ToLower();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String ToUpper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.ToUpper();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String Trim()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.Trim();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String TrimStart()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.TrimStart();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public GB2312String TrimEnd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return str.TrimEnd();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (obj == null)
InBlock.gif                
return false;
InBlock.gif            
else
InBlock.gif                
return _str.Equals(obj.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return _str;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override int GetHashCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return _str.GetHashCode();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool operator ==(GB2312String o1, GB2312String o2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (object.Equals(o1, null|| object.Equals(o2, null))
InBlock.gif                
return false;
InBlock.gif            
else
InBlock.gif                
return o1.Equals(o2);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool operator !=(GB2312String o1, GB2312String o2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return !(o1 == o2);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static GB2312String operator +(GB2312String o1, GB2312String o2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new GB2312String(o1.ToString() + o2.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static implicit operator string(GB2312String o)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return o.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static implicit operator GB2312String(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new GB2312String(str);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

转载于:https://www.cnblogs.com/oop/archive/2007/03/06/665131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值