向上帝打申请了,下辈子再也不做程序员!! 上帝批复:OK!

字符串函数

unit uzxString;
interface
 uses Classes,SysUtils;

  const DELIMITER_LF = #10;
  const DELIMITER_CR = #13 ;
  const DELIMITER_BLANK = ' ';
  const DELIMITER_COMMA = ',';
 { izx interface }
type
  zxCharSet = set of char;
  zxUpperCharSet = set of 'A'..'Z';
  zxLowerCharSet = set of 'a'..'z';
  zxDigitCharSet = set of '0'..'9';
type
  //用于判断该字符串是否是大小写敏感与否
  zxCase = (zxIgnoreCase,zxWithCase);
  //用于判断字符串的截取,消除空格等处理理是从左边,右边,还是两端处理
  zxSide = (zxLeftSide,zxRightSide,zxBothSides);
  //用于判断字符串的检索是从前向后,抑或相反
//  zxWay = (zxForward,zxBackward); 尚未实现,暂时取消

  TByteArray=array of byte;
  TCharArray=array of char;

type
  zxString=class(TObject)
  private
    fMum:String ;    //源字符串
    fCase:zxCase;    //是否区分大小写
//    fWay:zxWay;      //检索的方向
    fDelimiter:char; //分隔符
    class function readOnlySubstring(const mum:String;const pos:integer):string;
  protected
  //专门用于处理和抛出异常的方法
   class procedure handleException(const e:Exception);virtual;
   class procedure raiseException(const msg:String);virtual;
  //获取字符串的下标和上标 ,为了防止出现版本问题.
   function Top: integer;overload;
   function base: integer;overload;
   class function base(const mum:string):integer;overload;virtual;
   class function Top(const mum:String):integer;overload;virtual;

  //用于将变体类型转换成为字符的方法
   class function variantToString(const aVar:variant):String;virtual;
  public
  //判断是否是否已经实例化.
    function isNull:boolean;
  //将字符传赋值
    function assign(const str:String;const aCase:zxCase=zxWithCase):zxString;overload;
    function assign(const zx:zxString):zxString;overload;
  //计算字符串的哈希值
    function hashCode:cardinal;virtual;
  //将本对象装换成为字符串类型.
    function toString:WideString;overload;
  //实现zxString对象的序列化.
    function LoadFromStream(const stream:TStream):zxString;
    function SaveToStram(const stream:TStream):zxString;
    function LoadFromFile(const filename:String):zxString;
    function SaveToFile(const filename:String):zxString;

  //实现字符串的相同的比较  类方法和实例方法
    function equal(const str:String):boolean;overload;
    class function equal(const str1,str2:String;const aCase:zxCase=zxWithCase):boolean;overload;
  //实现字符串的比较 当字符串1<字符串2是返回 值小于0 否则 返回值>0 如果相等则返回0
    function compare(const str:String):integer;overload;
    class function compare(const str1,str2:String;const aCase:zxCase=zxWithCase):integer;overload;

  //将str插入到字符对象指定的位置.
    function insert(const aVar:Variant;const loc:integer):zxString;overload;
    class function insert(const mum,part:String;const loc:integer):string;overload;
  //将str字符串追加到本字符对象的最后
    function append(const aVar:variant):zxString;

    //根据是否指定大小写删除字符串中的第一个子串
    class function delete(const mum:String;const part:String;const aCase:zxCase=zxWithCase):String; overload;
    function delete(const str:String):zxString;overload;
    //根据位置和长度删除固定长度的子串
    class function delete(const mum:String;const beginPos,count:Integer):String; overload;
    function delete(const beginPos,count:integer):zxString;overload;
    //根据指定的大小写删除所有指定的子串
    class function deleteAll(const mum:String;const part:String;const aCase:zxCase=zxWithCase):String;overload;
    //通过设置case 可以指定删除的子串是否根据大小写自定
    function deleteAll(const str:String):zxString;overload;

    //根据是否指定大小写替换第一个的子串
    class function replace(const mum:String;const new,old:String;const aCase:zxCase=zxWithCase):String;overload;
    function replace(const new ,old:String):zxString;overload;
    //根据是否指定大小写替换自定位置的字符串
    class function replace(const mum,new:String;const beginPos,count:integer):String;overload;
    function replace(const beginPos,count:integer;const new:String):zxString;overload;
    //根据是否指定大小写替换所有指定的子字符串
    class function replaceAll(const mum, aOld, aNew:String;const aCase:zxCase=zxWithCase):String;overload;
    class function replaceAll(const mum:String;const aOld,aNew:char;const aCase:zxCase=zxWithCase):String;overload;
    function replaceAll(const old,new:String):zxString;overload;

     //获取某个指定字符的大小写全集 如['a','A']
    class function getLowerUpper(const aChar:char):zxCharSet;

    //根据检索返回某个子串的开始位置
    function locate(const str:string):integer;overload;
    class function locate(const mum,part:String;const aCase:zxCase=zxWithCase):integer;overload;

    //根据检索返回某个子串的出现的序数
    function indexOf(const str:string):integer;overload;
    class function indexOf(const mum,part:String;const index:integer=1;const aCase:zxCase=zxWithCase):integer;overload;
    {实现 特殊的字符串类的功能}
    //获取字符串的长度
    class function length(const str:string):integer;overload;
    function length:integer;overload;

    //设置字符串的是否是区分大小写的,不采用属性的原因是属性不能返回值.
    function setCase(const aCase:zxCase):zxString;
    function getCase:zxCase;

    //字节数
    class function CharCount(const mum:String):Integer;overload;
    function charCount:integer;overload;
    //unicode 字符数 中文汉字计数为一个字
    class function WideCharCount(const mum:String):integer;overload;
    function WideCharCount:integer;overload;
    //根据起始位置获取子字符串
    class function substring(const mum:String;const beginPos,count:Integer):String;overload;
    function substring(const beginPos,count:integer):String;overload;
    //将字符串倒置
    class function reverse(const mum:String):String;overload;
    function reverse:zxString;overload;
    //根据side的设置将字符串的两边空格除去
    class function trim(const mum:String;const side:zxSide=zxBothSides):String;overload;
    function trim(const aSide:zxSide=zxBothSides):zxString;overload;
    //设置字符和字符串的大小写
    class function upperCase(const mum:String):String;overload;
    class function upperCase(const mum:char):char;overload;
    class function lowerCase(const mum:char):char;overload;
    function upperCase:zxString;overload;
    class function lowerCase(const mum:String):String;overload;
    function lowerCase:zxString;overload;
    //判断是否包含子字符串
    class function contain(const mum,part:String;const aCase:zxCase=zxWithCase):boolean;overload;
    function contain(const part:String):boolean;overload;
    function contain(const part:zxString):boolean;overload;
    //根据位置返回单个字符已经考虑到中文字符 16位
    class function getWideChar(const mum:String;const loc:integer):wideChar;overload;
    function getWideChar(const loc:integer):WideChar;overload;
    //根据位置返回单个字符 8位
    class function getChar(const mum:String;const loc:integer):Char;overload;
    function getChar(const loc:integer):Char;overload;

   //根据位置设定该位置的字符 所有的char,wideChar 均采用该函数
    class function setWideChar(const mum:WideString;const aChar:WideChar;const loc:integer):WideString;overload;
    function setWideChar(const aChar:WideChar;const loc:integer):zxString;overload;

    //获取子字符串的个数
    class function substringCount(const mum ,part:String;const aCase:zxCase):Integer;overload;
    function substringCount(const part:zxString):Integer;overload;
    function substringCount(const part:String):integer;overload;

    //判断字符串是否以子字符串开始
    class function startWith(const mum,str:String;const aCase:zxCase=zxWithCase):boolean;overload;
    function startWith(const obj:zxString):boolean;overload;
    function startWith(const str:String):boolean;overload;
    //判断字符串是否以子字符串结束
    class function endWith(const mum,str:String;const aCase:zxCase=zxWithCase):boolean;overload;
    function endWith(const obj:zxString):boolean;overload;
    function endWith(const str:String):boolean;overload;

    //将变体类型转换成为字符串 支持字符,字符串,整数,浮点数,布尔,日期类型
    class function asString(const aVar:Variant):String;overload;virtual;
    //转换成为字符数组
    class function asCharArray(const mum:String):TCharArray;
    //返回将字符串复制count次的字符串
    class function repeatString(const mum:String;const count:integer):string;overload;
    function repeatString(const count:integer):zxString;overload;
    //将首字母大写.
    class procedure capitalInitial(var mum:String);overload;
    function capitalInitial:zxString;overload;
    //除去多余的字符 其中wipechar为多余的字符,wrap为分隔符,分隔符内的字符的多余字符并不去除 如'this    is  '->'this is '
    class function removeRedundantChar(const mum:string; const wipeChar,wrap:char;const aCase:zxCase=zxWithCase):String ;overload;
    function removeRedundantChar(const wipeChar,wrap:char):zxString ;overload;
    //根据分隔符将字符串分割成紧缩单词,不留空格 aDelimiter为分隔符,wipechar为要去除的字符.
    class function splitToCompactWords(aValue : string; aDelimiter,wipeChar : Char):TStrings;overload;
    function splitToCompactWords:TStrings; overload;
    //根据分隔符将字符串分割成松散的单词,保留空格
    class function splitToLooseWords(aValue : string; aDelimiter: Char):TStrings;overload;
    function splitToLooseWords:TStrings;overload;
    //设置分隔符
    function setDelimiter(const aDelimiter:char):zxString;
    function getDelimiter:char;
    //清除字符串内容为''
    function clear:zxString;
    //改变常量的值.这只是一个彩蛋而已(:
    class procedure ChangeConstant(const Constant; var Value; Size: Integer);
    //显示字符串对象的内容
    function show:zxString;overload;
    function show(const aVar:variant):zxString;overload;
    //返回字符串unicode的长度,
   constructor create(const mum:String='';const aCase:zxCase=zxWithCase{;const aWay:zxWay=zxForward});
 end;

implementation

uses strUtils,Dialogs ;

{ ----------------------------  zxString   ----------------------------------}
constructor zxString.create(const mum:String;const aCase:zxCase{;const aWay:zxWay=zxForward});
begin
  inherited create;
  fCase:=aCase;
//  fWay := aWay ;
  fMum:=mum;
  fDelimiter:= DELIMITER_BLANK ;
end;

function zxString.isNull: boolean;
begin
  if assigned(self) then result:= true else result := false ;
end;

function zxString.assign(const str: String;const aCase:zxCase):zxString;
begin
   fMum := str;
   fCase := aCase ;
   result := self ;
end;

function zxString.assign(const zx: zxString): zxString;
begin
  fMum := zx.fMum;
  fCase := zx.fCase ;
  result := self ;
end;

function zxString.toString: WideString;
begin
   result:= fMum ;
end;

class function zxString.CharCount(const mum: String): Integer;
var
   pSrc,pDst:PChar;
   tmp:String;
begin
    tmp:=mum;
    pSrc:=PChar(tmp);
    pDst:=pSrc;
    while pDst^<>#0 do
    inc(pDst);
    result:=pDst-pSrc;
end;

function zxString.CharCount: integer;
begin
  result := charCount(fMum);
end;

class function zxString.WideCharCount(const mum: String): integer;
var
  Unicode:wideString;
begin
  Unicode := WideString(mum);
  result :=  system.length( unicode );
end;

function zxString.WideCharCount: integer;
begin
  result :=WideCharCount(fMum);
end;

function zxString.compare(const str: String): integer;
begin
    result:= compare(fMum,str,fCase);
end;

class function zxString.compare(const str1, str2: String;
  const aCase: zxCase=zxWithCase): integer;
begin
  case aCase of
  zxWithCase:     result := sysUtils.compareStr(str1,str2) ;
  zxIgnoreCase:   result := sysUtils.CompareText(str1,str2) ;
  end;
end;

function zxString.contain(const part: String): boolean;
begin
  result := contain(fMum , part ,fCase );
end;

class function zxString.contain(const mum, part: String;const aCase:zxCase=zxWithCase): boolean;
begin
  case aCase of
    zxWithCase: result := strUtils.AnsiContainsStr(mum,part);
    zxIgnoreCase: result := strUtils.AnsiContainsText(mum,part);
  end;
end;

function zxString.contain(const part: zxString): boolean;
begin
  result := contain(fMum , part.fMum );
end;

class function zxString.delete(const mum: String; const part: String;const aCase:zxCase=zxWithCase):String;
var
  loc:integer;
begin
   result := mum ;
   loc := locate(result,part,aCase);
   if loc >0 then system.delete(result,loc,length(part));
end;

function zxString.delete(const str: String): zxString;
var
   loc:integer;
begin
  loc := locate(str);
  if loc >0 then delete(fMum,loc,length(str));
  result := self ;
end;

function zxString.delete(const beginPos, count: integer): zxString;
begin
  delete(fMum, beginPos,count) ;
  result := self ;
end;

class function zxString.delete(const mum: String; const beginPos,
  count: Integer):String;
begin
  result := mum ;
  system.Delete(result,beginPos,count);
end;

class function zxString.deleteAll(const mum: String; const part: String;const aCase:zxCase=zxWithCase):String;
var
  pos:integer;
begin
  result := mum ;
  pos := locate(mum,part,aCase);
  while pos>0 do
  begin
    delete(result,part,aCase);
    pos := locate(result,part,aCase);
  end;
end;

function zxString.deleteAll(const str: String): zxString;
begin
  deleteAll(fMum,str,fCase);
  result := self;
end;

class function zxString.endWith(const mum, str: String;const aCase:zxCase): boolean;
begin
  case aCase of
    zxWithCase:  result :=   strUtils.AnsiEndsStr(str,mum);
    zxIgnoreCase:result :=   strUtils.AnsiEndsText(str,mum);
  end;
end;

function zxString.endWith(const str: String): boolean;
begin
  result:=endWith(fMum,str,fCase);
end;

function zxString.endWith(const obj: zxString): boolean;
begin
  result:=endWith(fMum,obj.fMum,fCase);
end;

function zxString.equal(const str: String): boolean;
begin
  result := equal(fMum,str,fCase);
end;

class function zxString.equal(const str1,str2: String;
  const aCase: zxCase): boolean;
begin
  result := compare(str1,str2,aCase)=0;
end;

class function zxString.getChar(const mum: String; const loc: integer): Char;
begin
  if loc<1 then result:= mum[1]
  else if loc > system.length(AnsiString(mum)) then result:=mum[system.length(AnSiString(mum))]
  else result := mum[loc] ;
//  if (loc > 0) and (loc < length(mum)) then result:= mum[loc] else result := #0 ;
end;

function zxString.getChar(const loc: integer): Char;
begin
  result := getChar(fMum,loc);
end;

function zxString.getCase: zxCase;
begin
  result := fCase ;
end;

function zxString.getWideChar(const loc: integer): WideChar;
begin
  result := getWideChar(fMum,loc);
end;

class function zxString.getWideChar(const mum: String;
  const loc: integer): Widechar;
var tmp:WideString;
begin
  tmp := WideString(mum) ;
  if loc<1 then result:= tmp[1]
  else if loc > system.length(tmp) then result:=tmp[system.length(tmp)]
  else result := tmp[loc] ;
//  if (loc > 0) and () then result:= tmp[loc] else result := #0 ;
end;

function zxString.hashCode: cardinal;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(fMum) do
    Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor Ord(fMum[i]);
end;

function zxString.insert(const aVar:Variant; const loc: integer):zxString;
begin
  fMum := insert(fMum,VariantToString(aVar),loc);
  result := self ;
end;

class function zxString.insert(const mum,part: String;const loc: integer):string;
begin
  result := mum ;
  system.Insert(part,result,loc);
end;

class function zxString.locate(const mum, part: String;const aCase:zxCase=zxWithCase): integer;
begin
  case aCase of
    zxWithCase:    result := sysUtils.AnsiPos(part,mum);
    zxIgnoreCase:  result :=  sysUtils.AnsiPos(lowerCase(part),lowerCase(mum));
  end;
end;

function zxString.locate(const str: string): integer;
begin
  result := locate(fMum , str , fCase);
end;

function zxString.lowerCase: zxString;
begin
    fMum := lowerCase( fMum );
    result := self ;
end;

class function zxString.lowerCase(const mum: String):String;
begin
    result := sysUtils.LowerCase( mum ) ;
end;

function zxString.loadFromFile(const filename: String):zxString;
var
  tmp:TStrings;
begin
  tmp:= tStringList.Create;
  fMum := '' ;
  try
    try
      tmp.LoadFromFile(fileName);
      fMum := tmp.Text;
    except
      on e:Exception do handleException(e);
    end;
  finally
    tmp.Free;
  end;
  result := self ;
end;

function zxString.loadFromStream(const stream: TStream):zxString;
var
  tmp:TStrings;
begin
  tmp:= tStringList.Create;
  fMum := '' ;
  try
    try
      tmp.LoadFromStream(stream);
      fMum := tmp.Text;
    except
      on e:Exception do handleException(e);
    end;
  finally
    tmp.Free;
  end;
  result := self ;
end;


function zxString.replace(const beginPos, count: integer;
  const new: String): zxString;
begin
  fMum := replace(fMum,new,beginPos,count);
end;

class function zxString.replace(const mum,new:String;const beginPos,count:integer): string;
var
  tmp:String;
begin
  tmp := mum ;
  delete(tmp,beginPos,count);
  insert(tmp,new,beginPos);
  result := tmp ;
end;

function zxString.replace(const new,old: String): zxString;
begin
     fMum := replace(fMum,new , old , fCase );
     result := self ;
end;

class function zxString.replace(const mum: String; const new, old: String;const aCase:zxCase=zxWithCase):String;
begin
    case aCase of
      zxWithCase:      result := sysUtils.stringReplace(mum,old,new,[]);
      zxIgnoreCase:    result := sysUtils.stringReplace(mum,old,new,[rfIgnoreCase]);
    end;
end;

function zxString.replaceAll(const old,new: String): zxString;
begin
  fMum := replaceAll(fMum,old,new,fCase);
  result := self ;
end;

class function zxString.replaceAll(const mum, aOld, aNew: String;
  const aCase: zxCase): String;
begin
    result := mum ;
    case aCase of
      zxWithCase:     result:= sysUtils.stringReplace(result,aOld,aNew,[rfReplaceAll]);
      zxIgnoreCase:   result := sysUtils.stringReplace(result,aOld,aNew,[rfReplaceAll, rfIgnoreCase]);
    end;
end;

class function zxString.reverse(const mum: String): String;
begin
  result := strUtils.ReverseString(mum);
end;

function zxString.reverse: zxString;
begin
  fMum := reverse( fMum );
  result := self;
end;

function zxString.setCase(const aCase: zxCase):zxString;
begin
  fCase := aCase ;
  result := self ;
end;

class function zxString.setWideChar(Const mum: WideString; const aChar: WideChar;
  const loc: integer):wideString;
begin
   result := mum ;
   if (loc < system.length(result) ) and (loc >0 ) then  result[loc] := aChar ;
end;

function zxString.setWideChar(const aChar: WideChar; const loc: integer): zxString;
var tmp:WideString;
begin
   tmp := WideString(fMum) ;
   fMum := setWideChar(tmp,aChar,loc);
   result := self;
end;

function zxString.startWith(const obj: zxString): boolean;
begin
  result := startWith(obj.fMum);
end;

function zxString.startWith(const str: String): boolean;
begin
  result := startWith(fMum,str,fCase);
end;

class function zxString.startWith(const mum, str: String;const aCase:zxCase): boolean;
begin
  case aCase of
    zxWithCase:   result := strUtils.AnsiStartsStr(str,mum);
    zxIgnoreCase: result := strUtils.AnsiStartsText(str,mum);
  end;
end;

class function zxString.subString(const mum: String; const beginPos,
  count: Integer): String;
begin
  result := system.copy(mum,beginPos,count);
end;

function zxString.substring(const beginPos, count: integer): String;
begin
  result := fMum ;
  substring( result ,beginPos, count);
end;

class function zxString.substringCount(const mum, part: String;const aCase:zxCase): Integer;
begin
  if (Length(mum) = 0) or (Length(part) = 0) or (locate(mum, part,aCase) < 0) then
    Result := 0
  else
    Result := (Length(mum) - Length(replaceAll(mum,part,'',aCase) ) ) div Length(part);
end;

function zxString.subStringCount(const part: zxString): Integer;
begin
  result := subStringCount( part.fMum );
end;

function zxString.subStringCount(const part: String): integer;
begin
  result := substringCount(fMum,part,fCase);
end;

function zxString.trim(const aSide:zxSide): zxString;
begin
  fMum := trim(fMum,aSide);
  result := self;
end;

class function zxString.trim(const mum: String;const side:zxSide):String;
begin
  case side of
    zxLeftSide:   result := SysUtils.TrimLeft( mum );
    zxRightSide:   result := SysUtils.TrimRight( mum );
    zxBothSides:   result := SysUtils.Trim( mum );
  end;
end;

function zxString.upperCase: zxString;
begin
  fMum := upperCase(fMum);
  result:=self;
end;

class function zxString.upperCase(const mum: String):String;
begin
  result := SysUtils.UpperCase(mum);
end;

function zxString.SaveToFile(const filename: String):zxString;
var
  tmp:TStrings;
begin
  tmp:= tStringList.Create;
  tmp.Add(fMum);
  try
    try
      tmp.SaveToFile(filename);
    except
      on e:Exception do handleException(e);
    end;
  finally
    tmp.Free;
  end;
  result := self ;
end;

function zxString.SaveToStram(const stream: TStream):zxString;
var
  tmp:TStrings;
begin
  tmp:= tStringList.Create;
  tmp.Add(fMum);
  try
    try
      tmp.SaveToStream(stream);
    except
      on e:Exception do handleException(e);
    end;
  finally
    tmp.Free;
  end;
  result := self ;
end;

class function zxString.length(const str: string): integer;
begin
  result := system.length(str);
end;

function zxString.length: integer;
begin
  result := length(fMum);
end;

function zxString.indexOf(const str: string): integer;
begin
  result := indexof(fMum,str,1,fCase);
end;

class function zxString.indexOf(const mum, part: String;const index:integer=1;
  const aCase: zxCase=zxWithCase): integer;//返回当前位置
var
   tmp :String;
   count:integer;
   i:integer;
begin
  count := substringCount(mum,part,aCase);
  if count>0 then begin
    result := locate(mum,part,aCase);
    tmp := readonlySubstring(mum,result);
    for i:=1 to count do
    begin
      result:=locate(tmp,part,aCase) ;
      tmp :=  readonlysubString(mum,result);
    end;
  end else result := -1 ;
end;

class function zxString.asCharArray(const mum: String): TCharArray;
begin
  result := TCharArray(Pchar(mum));
end;

class function zxString.repeatString(const mum: String;
  const count: integer): string;
begin
  result := dupeString(mum,count);
end;

function zxString.repeatString(const count: integer): zxString;
begin
  fMum := repeatString(fMum,count);
  result:= self;
end;

class procedure zxString.capitalInitial(var mum: String);
var
  i:integer;
  flag:boolean;
  ch:PChar;
begin
  ch := PChar(mum);
  flag := False;
  if (ch^ in ['a'..'z']) then ch^:=Char(ORD(ch^)-32)
  else if ch^ =' ' then   flag:=True;
  for i:=base(mum) to top(mum) do
  begin
    INC(ch);
    if ch^=' ' then
    begin
      flag:= true ;
      continue;
    end;
    if flag and (ch^ in ['a'..'z']) then
    begin
      ch^:= Char(ORD(ch^)-32);
      flag:=False;
    end else       flag:=False;
  end;
end;

function zxString.capitalInitial: zxString;
begin
   capitalInitial(fMum);
   result := self ;
end;

function zxString.base: integer;
begin
  result := base(fMum);
end;

function zxString.top: integer;
begin
   result := top(fMum);
end;

class function zxString.base(const mum: string): integer;
begin
  result := 1;
end;

class function zxString.top(const mum: String): integer;
begin
  result := length(mum);
end;

function zxString.getDelimiter: char;
begin
  result := fDelimiter ;
end;

function zxString.setDelimiter(const aDelimiter:char): zxString;
begin
  fDelimiter := aDelimiter;
  result := self;
end;

class procedure zxString.ChangeConstant(const Constant; var Value; Size: Integer);
begin
  system.Move((@Value)^, (@Constant)^, Size);
end;

function zxString.show: zxString;
begin
  show(fMum) ;
  result := self ;
end;


//将分隔符两边的空格全部去除 如 "aaa   " ,  asd ,123.2  , 1 =>"aaa  ",asd,123.2,1
//默认wipeChar =' ';
class function zxString.splitToCompactWords(aValue : string; aDelimiter,wipeChar: Char):TStrings;
var
  X : Integer;
  S : string;
begin
  Result := TStringList.Create;
  S := '';
  for X:=1 to Length(aValue) do
  begin
    if aValue[X]=aDelimiter
    then begin
      if S='' then continue else Result.Add(trim(S));
      S:='' ;
    end else S:=S + aValue[X];
  end;
  if S <> '' then Result.Add(trim(S));
end;

//Splits a delimited text line into TStrings (does not account for stuff in quotes but it should)
//不将将分隔符两边的空格去除 如 "aaa   " ,  asd ,123.2  , 1 =>"aaa  "  ,  asd,  123. 2,1
class function zxString.splitToLooseWords(aValue : string; aDelimiter : Char):TStrings;
var
  X : Integer;
  S : string;
begin
  result := TStringList.Create;
  S := '';
  for X:=1 to Length(aValue) do
  begin
    if aValue[X]=aDelimiter
    then begin
      if S='' then continue else Result.Add(S);
      S:='' ;
    end else S:=S + aValue[X];
  end;
  if S <> '' then Result.Add(S);
end;

function zxString.clear: zxString;
begin
  fMum := '' ;
  result := self ;
end;

function zxString.splitToCompactWords: TStrings;
begin
  result := splitToCompactWords(fMum,fDelimiter,DELIMITER_BLANK);
end;

function zxString.splitToLooseWords: TStrings;
begin
  result := splitToLooseWords(fMum,fDelimiter);
end;

class function zxString.readOnlySubstring(const mum: String;
  const pos: integer): string;
begin
  if (pos < 0 ) then result := PChar(mum)
  else if (pos < length(mum) ) then result := PChar(mum) + pos
  else result:=PChar(mum) + length(mum);
end;

class procedure zxString.handleException(const e: Exception);
begin
  showMessage(e.Message);
end;

class procedure zxString.raiseException(const msg: String);
begin
  raise exception.Create(msg);
end;

class function zxString.upperCase(const mum: char): char;
begin
  result := mum ;
  if result in ['a'..'z'] then  Dec(Result, Ord('a') - Ord('A'));
end;

class function zxString.lowerCase(const mum: char): char;
begin
  result := mum ;
  if result in ['A'..'Z'] then  INC(Result, Ord('a') - Ord('A'));
end;

class function zxString.getLowerUpper(const aChar: char): zxCharSet;
begin
  case aChar of
    'a'..'z': result := [aChar,upperCase(aChar)];
    'A'..'Z': result := [lowerCase(aChar),aChar];
  end;
end;

class function zxString.replaceAll(const mum: String; const aOld, aNew: char;
   const aCase: zxCase): String;
var
  i:integer;
begin
  result := mum ;
  for i:=1 to length(mum) do
  begin
   case aCase of
     zxWithCase:
       if result[i]=aOld then result[i]:= aNew ;
     zxIgnoreCase:
      if result[i] in getLowerUpper(aOld) then result[i]:= aNew ;
   end;//case
  end; //for
end;

//消除字符串中多余的字符,在特定边界中的如' ' " " ,目前并不对多种情况同时消除多余字符.
class function zxString.removeRedundantChar(const mum:string; const wipeChar,wrap:char;const aCase:zxCase): String;
var i:integer;
    delCharSet:zxCharSet;
    inDelimiter:boolean ;
    delCount:integer;
    beginPos:integer;
    size :integer;
begin
  beginPos := 0 ;
  i:=1 ;
  delCount := 0 ;
  result := mum ;
  size :=length( result ) ;
  inDelimiter := false ;
  if aCase = zxWithCase then delCharSet:=[wipeChar] else  delCharSet:=getLowerUpper(wipeChar);
  while i<=size do
  begin
    INC(i);
    if result[i]=wrap then
      if inDelimiter
      then inDelimiter:=false
      else begin
        inDelimiter:= true;
      end
    else begin
      if inDelimiter then continue ;
      if result[i] in delCharSet then
      begin
        if beginPos=0 then beginPos:=i else INC(delCount);
        if i=size then result:=delete(result,beginPos,delCount);
      end else begin
        if delCount>0 then begin
          result:=delete(result,beginPos,delCount);
          DEC(size,delCount);
          i:=beginPos ;
        end ;
        beginPos := 0 ;
        delCount := 0 ;
      end;//if

    end;//if
  end;//while
end;

function zxString.removeRedundantChar(const wipeChar,
  wrap: char): zxString;
begin
  fmum := removeRedundantChar(fmum,wipeChar,wrap,fCase);
  result := self ;
end;

function zxString.append(const aVar: variant): zxString;
begin
  fMum := fMum + variantToString( aVar );
  result := self ;
end;

class function zxString.variantToString(const aVar: variant): String;
begin
  //可以将intToStr,floatToStr 等方法声明为virtual 就可以定制转换字符的格式.
  case TVarData(aVar).VType of
    varEmpty    ,// = $0000;
    varNull     :// = $0001;
      result := '' ;
    varByte     ,// = $0011;
    varWord     ,// = $0012;
    varLongWord ,// = $0013;
    varInt64    ,// = $0014;
    varShortInt ,// = $0010;
    varSmallint ,// = $0002;
    varInteger  ,// = $0003;
    varSingle   :// = $0004;
      result:= intToStr(aVar);
    varDouble   ,// = $0005;
    varCurrency :// = $0006;
      result:= floatToStr(aVar);
    varDate     :// = $0007;
     result:= FormatDateTime('yyyy-mm-dd hh:mm:ss',aVar);
    varBoolean  :// = $000B;
     if aVar then result:= 'True' else result:= 'False';
    varOleStr   ,// = $0008;
    varStrArg   ,// = $0048;
    varString   :// = $0100;
     result := aVar ;
    varAny      ,// = $0101;
    varTypeMask ,// = $0FFF;
    varArray    ,// = $2000;
    varByRef    ,// = $4000;
    varDispatch ,// = $0009;
    varError    ,// = $000A;
    varVariant  ,// = $000C;
    varUnknown  :begin// = $000D;
      result :='';
      raiseException('无法将变体类型变量转换成为字符串.');
    end;
  end
end;

class function zxString.asString(const aVar: Variant): String;
begin
  result := variantToString(aVar);
end;

function zxString.show(const aVar: variant): zxString;
begin
  showMessage(asString(aVar));
  result := self ;
end;

initialization
//进行初试化的工作
finalization
//进行清理的工作
end.

posted @ 2009-03-25 10:09  zywuhao  Views(766)  Comments(0)    收藏  举报