增强字符串
来源:互联网 发布:网络消费安全标语 编辑:程序博客网 时间:2024/04/29 17:39
一个自定义类,用于大规模的字符串连接,如拼接SQL语句。用流技术实现的,很好呦!!
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace SuperString
{
/// <summary>
/// 创 建 者: superhood
/// 内容描述: 增强字符串类
/// </summary>
public class CSuperString
{
/// <summary>
/// 功能简述: 运算符重载
/// </summary>
/// <param name="Left">左参数</param>
/// <param name="Right">右参数</param>
/// <returns>文本类</returns>
public static CSuperString operator + (string Left,CSuperString Right)
{
byte[] btyLeft = Encoding.Default.GetBytes(Left);//返回左参数数组
byte[] btyRight = new Byte[Right.iLength];//返回右参数数组
Right.iTextLength += Left.Length;//设置右参数文本长度
Right.iLength = btyLeft.Length + btyRight.Length;//设置右参数字节长度
Right.memStrm.Position = 0;//将右参数流位置置0
Right.memStrm.Read(btyRight,0,btyRight.Length);//将右参数数据读出
Right.memStrm.Position = 0;//将右参数流位置置0
Right.memStrm.Write(btyLeft,0,btyLeft.Length);//将字符串(字节数组)写入右参数
Right.memStrm.Write(btyRight,0,btyRight.Length);//将右参数原有信息写回(加在左参数字符串后)
return Right;//返回右参数
}
/// <summary>
/// 功能简述: 运算符重载
/// </summary>
/// <param name="Left">左参数</param>
/// <param name="Right">右参数</param>
/// <returns>文本类</returns>
public static CSuperString operator + (CSuperString Left,string Right)
{
byte[] btyRight = Encoding.Default.GetBytes(Right);//将右参数(字符串)转换为字节数组
Left.memStrm.Position = Left.iLength;//设置左参数流的位置
Left.memStrm.Write(btyRight,0,btyRight.Length);//将右参数字符串写入流
Left.iTextLength += Right.Length;//设置左参数文本长度
Left.iLength += btyRight.Length;//设置左参数字节长度
return Left;//返回左参数
}
/// <summary>
/// 功能简述: 运算符重载
/// </summary>
/// <param name="Left">左参数</param>
/// <param name="Right">右参数</param>
/// <returns>文本类</returns>
public static CSuperString operator + (CSuperString Left,CSuperString Right)
{
byte[] btyRight = new Byte[Right.iLength];//声明字节数组(右参数)
Right.memStrm.Position = 0;//将右参数流位置置0
Right.memStrm.Read(btyRight,0,btyRight.Length);//将右参数(字符串)转换为字节数组
Left.memStrm.Position = 0;//将左参数流位置置0
Left.memStrm.Write(btyRight,0,btyRight.Length);//将右参数字符串写入流
Left.iTextLength += Right.iTextLength;//设置左参数文本长度
Left.iLength += Right.iLength;//设置左参数字节长度
return Left;//返回左参数
}
/// <summary>
/// 功能简述: 流中有效字节长度
/// </summary>
private int iLength = 0;
/// <summary>
/// 功能简述: 流中文本长度
/// </summary>
private int iTextLength = 0;
/// <summary>
/// 功能简述: 内存流
/// </summary>
private MemoryStream memStrm;
/// <summary>
/// 功能简述: 构造函数
/// </summary>
public CSuperString()
{
memStrm = new MemoryStream();//初始化流
}
/// <summary>
/// 功能简述: 构造函数
/// </summary>
/// <param name="DefaultLength">默认长度(以字节为单位)</param>
public CSuperString(int DefaultLength)
{
memStrm = new MemoryStream(DefaultLength);//初始化流
}
/// <summary>
/// 功能简述: 属性,字节长度
/// </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);//字符串转换为字节数组
memStrm.Position = iLength;//设置流的位置
memStrm.Write(btyDate,0,btyDate.Length);//将字符串写入流
iTextLength += Date.Length;//设置文本长度
iLength += btyDate.Length;//设置字节长度
}
/// <summary>
/// 功能简述: 返回文本
/// </summary>
/// <returns>返回字符串</returns>
public override string ToString()
{
memStrm.Position = 0;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
return Encoding.Default.GetString(btyDate);//将字节数组转换为字符串并返回
}
/// <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;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
strm.Write(btyDate,0,iLength);//将流内容写入文件
}
else
{//没有
memStrm.WriteTo(strm);//将流中文本写入文件
}
strm.Close();//关闭文件
}
/// <summary>
/// 功能简述: 将字符串写入流
/// </summary>
/// <param name="strm">流</param>
public void WriteToStream(Stream strm)
{
//判断流长度用来确定流中是否有冗余信息
if (memStrm.Length > iLength)
{//有
memStrm.Position = 0;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
strm.Write(btyDate,0,iLength);//将数组内容写入另一流
}
else
{//没有
memStrm.WriteTo(strm);//将流中文本写入另一流
}
}
/// <summary>
/// 功能简述: 清除流
/// </summary>
public void Clear()
{
iLength = 0;//将流字节长度设为0
iTextLength = 0;//将流文本长度设为0
}
}
}
- 增强字符串
- 增强字符串
- 字符串查找增强:findstr
- 增强的C/C++字符串格式化函数
- 增强J2ME的String能力-分割字符串
- cmd findstr 字符串查找增强使用说明
- python增强的格式化字符串format函数
- python中增强格式化字符串format函数
- 增强的格式化字符串format函数
- Oracle数据库中字符串和字符串匹配(INSTR增强)
- JavsScript 实现 StringBuilder 类, 增强字符串连接的性能。
- .NET 截取中英文混合的字符串。(增强版)
- C语言中字符替换为字符串(增强版)
- 关于String字符串的""值问题以及for增强循环
- 飘逸的python - 增强的格式化字符串format函数
- Atitit.ati str 字符串增强api
- 【Python开发】增强的格式化字符串format函数
- 飘逸的python - 增强的格式化字符串format函数
- MinUnit – 最小的c语言测试模板
- 异常处理方法
- 将javascript变量传递到PHP脚本中的方法
- 左键也能弹出菜单
- 为服务器控件添加javascript客户端事件
- 增强字符串
- JS版常见问题(二)兼容版本的无提示关闭窗口!
- 函数地址(不知道是否确切!)
- BCB存取图片等信息!
- 洗牌的一个C++类!
- MDB之Table输出到Word
- 设计并实现用于ASP.NET一个通用的数据存取层应用程序 原作者 Paul Abarham 翻译 cwxiao888@163.com
- delphi7从入门到精通之(一)
- Delphi 7从入门到精通之二