⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xstrings.pas

📁 我自己用的Delphi函数单元 具体说明见打包文件的HELP目录下面
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit xStrings;

interface

uses Sysutils;

const
	ASC_NULL 	    = Chr(0);
	ASC_TAB   	    = Chr(9);
	ASC_CR    	    = Chr(13);
	ASC_LF    	    = Chr(10);
	ASC_FF    	    = Chr(12);
	ASC_EOF   	    = Chr(26);
	ASC_ESC   	    = Chr(27);
	ASC_BACKSPACE   = Chr(08);
	ASC_SPACE       = Chr(32);
	ASC_CRLF        = Chr(13)+Chr(10);

  //DEFAULT_DELIMITERS = [' ', #9, #10, #13];
    DEFAULT_DELIMITERS = [#0..#$1F, ' ', '.', ',', '?', ':', ';', '(',')', '/', '\'];

type
    TFindMode = (fmMatchCase, fmWholeWord);
    TFindModes = set of TFindMode;
//------------------------------------------------------------------//
function IsEmptyString(const s:String): Boolean;
function IsNumberString(const s:string): Boolean;
function IsAlphaString(const s:string): Boolean;

//------------------------------------------------------------------//
function IsIncludeCRLF(const s: string): Boolean;
function CutTailCRLF(const s: string): String;

//------------------------------------------------------------------//
function RPos(const substr,s: String; ofs : Integer = 0): Integer;
function LPos(const substr,s: String; ofs : Integer = 0): Integer;

//------------------------------------------------------------------//
function PadL(const s : string; const len : Integer ; const c : Char = ASC_SPACE): string;
function PadC(const s : string; const len : Integer ; const c : Char = ASC_SPACE): string;
function PadR(const s : string; const len : Integer ; const c : Char = ASC_SPACE): string;

//------------------------------------------------------------------//
function LeftString (const s:string ; n : Integer):string;
function SubString  (const s:string ; Index,Count : Integer):string;
function RightString(const s:string ; n : Integer):string;

function FillString (const c : Char; const len : Integer): string;
function ReverseString(const s : string) : string;

function BracketString(const s: string): string;
function UnquoteString(const s: string): string;

function ReplaceString(const s: string; const Token, NewToken: string; bCaseSensitive: Boolean =True ): String;
function ReplaceIndexString(const s: string; const Substr: string; index, Count: Integer):String;

function RandomString (len : integer) : string;
function MatchString(s, substr: string; Modes: TFindModes = []): Integer;
function IsMatchString(const s, substr: string): Boolean;

//------------------------------------------------------------------//
function Before(const s, substr: string): string;
function After(const s, substr: string): string;
function Inside(const Search, Front, Back: string): string;
function LeftInside(const Search, Front, Back: string): string;
function RightSide(const Search, Front, Back: string): string;

//------------------------------------------------------------------//
function CutToken(var InTxt : String; SpaceChar : TSysCharSet = [',']) : String;
function FirstToken(var S: string; const Delimiter: string; Remove: Boolean): string;
function GetToken(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string;
function GetWords(S: string; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): Integer;

//------------------------------------------------------------------//
function ToStr(x:variant):string;
function ToVal(const s:string):double;
function ToAsc(const s : String ): integer;

//------------------------------------------------------------------//
function ParseRPLNo(var Msg: string): Integer;

implementation

//------------------------------------------------------------------//
//字符串为空串吗?
function IsEmptyString(const s:String): Boolean;
begin
    Result := length(trim(s)) = 0 ;
end;

//------------------------------------------------------------------//
//字符串为全数字组成吗?
function IsNumberString(const s:string): Boolean;
var
  i: Integer;
begin
  for i := 1 to Length(s) do
  begin
    if (not (s[i] in ['0'..'9'])) then
    begin
      Result := False;
      Exit;
    end;
  end;
  Result := True;
end;

//------------------------------------------------------------------//
//字符串为全字母组成吗?
function IsAlphaString(const s:string): Boolean;
var
  i: Integer;
begin
  for i := 1 to Length(s) do
  begin
    if (not (s[i] in ['a'..'z', 'A'..'Z'])) then
    begin
      Result := False;
      Exit;
    end;
  end;
  Result := True;
end;

//------------------------------------------------------------------//
//字符串尾部是否包含换行符
function IsIncludeCRLF(const s: string): Boolean;
var
  len: Integer;
begin
  len := Length(S);
  Result := (len >= 2) and (S[len - 1] = ASC_CR) and (S[len] = ASC_LF);
end;

//------------------------------------------------------------------//
//截去字符串尾部的回车换行符
function CutTailCRLF(const s: string): String;
var
  I: Integer;
begin
	Result:=s;
    I := 1;
    while I <= Length(Result) do
          if (Result[I] = ASC_CR) or (Result[I] = ASC_LF) then Delete(Result, I, 1)
          else Inc(I);
end;

//------------------------------------------------------------------//
//Pos函数的相对函数,从右边查找s串中的substr,返回正确位置,未找到时返回0。
//ofs取缺省值时搜索全串,否则只搜索1->ofs的字符串(注意ofs含义)。
function RPos(const substr, s: string; ofs: Integer = 0): Integer;
var
    i,n,m:Integer;
    ss:string;
begin
    Result:=0;
    if (ofs<0) or (ofs>Length(s)) then Exit;
    if ofs=0 then ofs:=Length(s);
    ss:=Copy(s,1,ofs);
    n:=Length(ss);
    m:=Length(substr);
    if m>n then Exit;
    for i:=n downto 1 do
        if (StrLComp(PChar(substr), PChar(ss)+i-1, m) = 0) then
        begin
            Result:=i;
            break;
        end;
end;

//------------------------------------------------------------------//
//Pos函数的相对函数,从左边查找s串中的substr,返回正确位置,未找到时返回0。
//ofs取缺省值时搜索全串,否则只搜索ofs->Length(s)的字符串(注意ofs含义)。
function LPos(const substr,s: String; ofs : Integer = 0): Integer;
begin
    Result:=0;
    if (ofs<0) or (ofs>Length(s)) then Exit;
    Result:=Pos(substr,Copy(s,ofs,(Length(s)-ofs)+1));
    if (Result>0) and (ofs>1) then Inc(Result,ofs-1);
end;

//------------------------------------------------------------------//
//将s向左扩展到len长度,扩展部分以c填充
function PadL(const s : string; const len : Integer ; const c : Char = ASC_SPACE): string;
var
	n: Integer;
begin
	n := Length (s);
	if n < len then
		Result := FillString (c, len - n) + s
	else
		Result := s;
end;

//------------------------------------------------------------------//
//将s向两端扩展到len长度,扩展部分以c填充
function PadC(const s : string; const len : Integer ; const c : Char = ASC_SPACE): String;
var
	n, m: Integer;
begin
	n := Length (s);
	if n < len then
	begin
		m := len - n;
		if Odd (m) then
			Result := FillString (c, m div 2) + s + FillString (c, m div 2 + 1)
		else
			Result := FillString (c, m div 2) + s + FillString (c, m div 2);
	end
	else
		Result := s;
end;

//------------------------------------------------------------------//
//将s向右扩展到len长度,扩展部分以c填充
function PadR(const s : string; const len : Integer ; const c : Char = ASC_SPACE): string;
var
	n: Integer;
begin
	n := Length (s);
	if n < len then
		Result := s + FillString (c, len - n)
	else
		Result := s;
end;

//------------------------------------------------------------------//
//取字符串左边n个字符
function LeftString(const s:string;n:Integer):string;
begin
    Result:=copy(s,1,n);
end;

//------------------------------------------------------------------//
//取字符串Index位置开始的n个字符,Copy更名
function SubString(const s:string;Index,Count:Integer):string;
begin
    Result:=copy(s,Index,Count);
end;

//------------------------------------------------------------------//
//取字符串右边n个字符
function RightString(const s:string;n:Integer):string;
var
    m:integer;
begin
    m:=length(s);
    if m<=n then Result:=s;
    m:=m-n+1;
    Result:=copy(s,m,n);
end;

//------------------------------------------------------------------//
//返回一个以c填充,长度为len的字符串
function FillString (const c : Char; const len : Integer): string;
begin
	SetLength (Result, len);
	FillChar (Result [1], len, c);
end;

//------------------------------------------------------------------//
//反转字符串
function ReverseString(const s : string) : string;
var
    i: integer;
begin
    Result:='';
    for i:=1 to length(s) do
        Result:=s[i]+Result;
end;

//------------------------------------------------------------------//
//字符串加方括号
function BracketString(const s: string): string;
begin
  Result := S;
  if (Result = '') or (Result[1] <> '[') then Result := '[' + Result;
  if Result[Length(Result)] <> ']' then Result := Result + ']';
end;


//------------------------------------------------------------------//
//去除字符串两端的引号
function UnquoteString(const S: string): string;
begin
  if S = '' then Exit;
  
  Result := S;
  
  if Result[1] = '"' then Delete(Result, 1, 1);
  if Result = '' then Exit;
  
  if Result[Length(Result)] = '"' then Delete(Result, Length(Result), 1);
end;

//------------------------------------------------------------------//
//字符串替换,不限字符串出现次数
//s:='Mary said than John likes Mary.';
//s:=ReplaceString(s,'Mary','Jannifer',True);
//返回Jannifer said than John likes Jannifer
function ReplaceString(const s: string; const Token, NewToken: string; bCaseSensitive: Boolean =True ): String;
var
  I         : Integer;
  sFirstPart: string;
begin
  Result:=s;
  if bCaseSensitive then
    I := AnsiPos(Token, Result)
  else
    I := AnsiPos(AnsiUpperCase(Token), AnsiUpperCase(Result));

  if I <> 0 then
  begin
    sFirstPart := Copy(Result, 1, I - 1) + NewToken; // 避免无穷递回
    Result := Copy(Result, I + Length(Token), Maxint);
  end;

  //Result := I <> 0;
  if I <> 0 then
  begin
    ReplaceString(Result, Token, NewToken, bCaseSensitive);
    Result := sFirstPart + Result;
  end;
end;

//------------------------------------------------------------------//
//简单字符串替换
//Index:替换起始位置
//Count:替换字符数
//s:='1234567'
//s:=ReplaceIndexString(s,'Piggy',2,4);
//返回 1Piggy67
function ReplaceIndexString(const S: string; const Substr: string; index, Count: Integer):String;
begin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -