之前有朋友問過小編一個問題,你對增強字符串這塊了解嗎?其實小編很多時候也只是稍微懂一點,就先不和大家分享了,如果有想了解的話,可以看看這個,是之前我收藏的! 一個自定義類,用于大規(guī)模的字符串連接,如拼接SQL語句。用流技術(shù)實現(xiàn)的,很好呦!! using System; using System.IO; using System.Text; using System.Windows.Forms; namespace SuperString { ///<summary> ///創(chuàng)建者:superhood ///內(nèi)容描述:增強字符串類 ///</summary> public class CSuperString { ///<summary> ///功能簡述:運算符重載 ///</summary> ///<param name="Left">左參數(shù)</param> ///<param name="Right">右參數(shù)</param> ///<returns>文本類</returns> public static CSuperString operator+(string Left,CSuperString Right) { byte[]btyLeft=Encoding.Default.GetBytes(Left);//返回左參數(shù)數(shù)組 byte[]btyRight=new Byte[Right.iLength];//返回右參數(shù)數(shù)組 Right.iTextLength+=Left.Length;//設(shè)置右參數(shù)文本長度 Right.iLength=btyLeft.Length+btyRight.Length;//設(shè)置右參數(shù)字節(jié)長度 Right.memStrm.Position=0;//將右參數(shù)流位置置0 Right.memStrm.Read(btyRight,0,btyRight.Length);//將右參數(shù)數(shù)據(jù)讀出 Right.memStrm.Position=0;//將右參數(shù)流位置置0 Right.memStrm.Write(btyLeft,0,btyLeft.Length);//將字符串(字節(jié)數(shù)組)寫入右參數(shù) Right.memStrm.Write(btyRight,0,btyRight.Length);//將右參數(shù)原有信息寫回(加在左參數(shù)字符串后) return Right;//返回右參數(shù) } ///<summary> ///功能簡述:運算符重載 ///</summary> ///<param name="Left">左參數(shù)</param> ///<param name="Right">右參數(shù)</param> ///<returns>文本類</returns> public static CSuperString operator+(CSuperString Left,string Right) { byte[]btyRight=Encoding.Default.GetBytes(Right);//將右參數(shù)(字符串)轉(zhuǎn)換為字節(jié)數(shù)組 Left.memStrm.Position=Left.iLength;//設(shè)置左參數(shù)流的位置 Left.memStrm.Write(btyRight,0,btyRight.Length);//將右參數(shù)字符串寫入流 Left.iTextLength+=Right.Length;//設(shè)置左參數(shù)文本長度 Left.iLength+=btyRight.Length;//設(shè)置左參數(shù)字節(jié)長度 return Left;//返回左參數(shù) } ///<summary> ///功能簡述:運算符重載 ///</summary> ///<param name="Left">左參數(shù)</param> ///<param name="Right">右參數(shù)</param> ///<returns>文本類</returns> public static CSuperString operator+(CSuperString Left,CSuperString Right) { byte[]btyRight=new Byte[Right.iLength];//聲明字節(jié)數(shù)組(右參數(shù)) Right.memStrm.Position=0;//將右參數(shù)流位置置0 Right.memStrm.Read(btyRight,0,btyRight.Length);//將右參數(shù)(字符串)轉(zhuǎn)換為字節(jié)數(shù)組 Left.memStrm.Position=0;//將左參數(shù)流位置置0 Left.memStrm.Write(btyRight,0,btyRight.Length);//將右參數(shù)字符串寫入流 Left.iTextLength+=Right.iTextLength;//設(shè)置左參數(shù)文本長度 Left.iLength+=Right.iLength;//設(shè)置左參數(shù)字節(jié)長度 return Left;//返回左參數(shù) } ///<summary> ///功能簡述:流中有效字節(jié)長度 ///</summary> private int iLength=0; ///<summary> ///功能簡述:流中文本長度 ///</summary> private int iTextLength=0; ///<summary> ///功能簡述:內(nèi)存流 ///</summary> private MemoryStream memStrm; ///<summary> ///功能簡述:構(gòu)造函數(shù) ///</summary> public CSuperString() { memStrm=new MemoryStream();//初始化流 } ///<summary> ///功能簡述:構(gòu)造函數(shù) ///</summary> ///<param name="DefaultLength">默認長度(以字節(jié)為單位)</param> public CSuperString(int DefaultLength) { memStrm=new MemoryStream(DefaultLength);//初始化流 } ///<summary> ///功能簡述:屬性,字節(jié)長度 ///</summary> public int Length { get { return iLength; } } ///<summary> ///功能簡述:屬性,文本長度 ///</summary> public int TextLength { get { return iTextLength; } } ///<summary> ///功能簡述:屬性,流長度 ///</summary> public int Capacity { get { return memStrm.Capacity; } set { if(value>=iLength) memStrm.Capacity=value; else memStrm.Capacity=iLength; } } ///<summary> ///功能簡述:向類中添加字符串 ///</summary> ///<param name="Date"></param> public void AddString(string Date) { byte[]btyDate=Encoding.Default.GetBytes(Date);//字符串轉(zhuǎn)換為字節(jié)數(shù)組 memStrm.Position=iLength;//設(shè)置流的位置 memStrm.Write(btyDate,0,btyDate.Length);//將字符串寫入流 iTextLength+=Date.Length;//設(shè)置文本長度 iLength+=btyDate.Length;//設(shè)置字節(jié)長度 } ///<summary> ///功能簡述:返回文本 ///</summary> ///<returns>返回字符串</returns> public override string ToString() { memStrm.Position=0;//設(shè)置流的位置 byte[]btyDate=new byte[iLength];//聲明字節(jié)數(shù)組 memStrm.Read(btyDate,0,iLength);//將流內(nèi)容讀入數(shù)組 return Encoding.Default.GetString(btyDate);//將字節(jié)數(shù)組轉(zhuǎn)換為字符串并返回 } ///<summary> ///功能簡述:將字符串寫入文件 ///</summary> ///<param name="FileName">文件名</param> public void WriteToFile(string FileName) { FileStream strm=new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);//初始化文件流 //判斷流長度用來確定流中是否有冗余信息 if(memStrm.Length>iLength) {//有 memStrm.Position=0;//設(shè)置流的位置 byte[]btyDate=new byte[iLength];//聲明字節(jié)數(shù)組 memStrm.Read(btyDate,0,iLength);//將流內(nèi)容讀入數(shù)組 strm.Write(btyDate,0,iLength);//將流內(nèi)容寫入文件 } else {//沒有 memStrm.WriteTo(strm);//將流中文本寫入文件 } strm.Close();//關(guān)閉文件 } ///<summary> ///功能簡述:將字符串寫入流 ///</summary> ///<param name="strm">流</param> public void WriteToStream(Stream strm) { //判斷流長度用來確定流中是否有冗余信息 if(memStrm.Length>iLength) {//有 memStrm.Position=0;//設(shè)置流的位置 byte[]btyDate=new byte[iLength];//聲明字節(jié)數(shù)組 memStrm.Read(btyDate,0,iLength);//將流內(nèi)容讀入數(shù)組 strm.Write(btyDate,0,iLength);//將數(shù)組內(nèi)容寫入另一流 } else {//沒有 memStrm.WriteTo(strm);//將流中文本寫入另一流 } } ///<summary> ///功能簡述:清除流 ///</summary> public void Clear() { iLength=0;//將流字節(jié)長度設(shè)為0 iTextLength=0;//將流文本長度設(shè)為0 } } } 字符串轉(zhuǎn)換 1如何將字串String轉(zhuǎn)換成整數(shù)int? A.有兩個方法: 1).int i=Integer.parseInt([String]);或 i=Integer.parseInt([String],[int radix]); 2).int i=Integer.valueOf(my_str).intValue(); 注:字串轉(zhuǎn)成Double,Float,Long的方法大同小異. 2如何將整數(shù)int轉(zhuǎn)換成字串String? A.有三種方法: 1.)String s=String.valueOf(i); 2.)String s=Integer.toString(i); 3.)String s=""+i; 注:Double,Float,Long轉(zhuǎn)成字串的方法大同小異. |
|