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

📄 xstrings.pas

📁 《Delphi深度历险》附书源码 Delphi学习书本
💻 PAS
字号:
unit xStrings;

interface

uses SysUtils, Classes, Windows, FileCtrl, Dialogs;

const
  DEFAULT_DELIMITERS = [' ', #9, #10, #13];
  
function GetToken(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string;
function CountWords(S: string; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): Integer;
function BracketString(const S: string): string;

procedure TruncateCRLF(var S: string);
function IsContainingCRLF(const S: string): Boolean;

function ReplaceString(var S: string; const Token, NewToken: string; bCaseSensitive: Boolean): Boolean;
procedure Simple_ReplaceString(var S: string; const Substr: string; index, Count: Integer);

function UnquoteString(const S: string): string;
function FirstToken(var S: string; const Delimiter: string; Remove: Boolean): string;
function AddTimeStamp(const S: string): string;

function PartialIndexOf(SL: TStrings; S: string; StartIndex: Integer; bForward: Boolean): Integer;
function CompositeStrings(SL: TStrings; const Delimiter: string): string;

function SafeLoadStrings(SL: TStrings; const Filename: string): Boolean;
procedure SafeSaveStrings(SL: TStrings; const Filename: string);

procedure RemoveDuplicates(SL: TStrings);
function ParseRPLNo(var Msg: string): Integer;

function RPos(const C: Char; const S: string): Integer;
function AnsiIPos(const Substr, S: string): Integer;
function MatchString(S, SubS: string; Options: TFindOptions): Integer;

implementation

function GetToken(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string;
var
  I, W, head, tail: Integer;
  bInWord         : Boolean;
begin
  I := 1;
  W := 0;
  bInWord := False;
  head := 1;
  tail := Length(S);
  while (I <= Length(S)) and (W <= index) do
  begin
    if S[I] in Delimiters then
    begin
      if (W = index) and bInWord then tail := I - 1;
      bInWord := False;
    end else
    begin
      if not bInWord then
      begin
        bInWord := True;
        Inc(W);
        if W = index then head := I;
      end;
    end;
    
    Inc(I);
  end;
  
  if bTrail then tail := Length(S);
  if W >= index then Result := Copy(S, head, tail - head + 1)
  else Result := '';
end;

function CountWords(S: string; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): Integer;
var
  bInWord: Boolean;
  I      : Integer;
begin
  Result := 0;
  I := 1;
  bInWord := False;
  while I <= Length(S) do
  begin
    if S[I] in Delimiters then bInWord := False
    else
    begin
      if not bInWord then
      begin
        bInWord := True;
        Inc(Result);
      end;
    end;
    
    Inc(I);
  end;
end;

function IsContainingCRLF(const S: string): Boolean;
var
  len: Integer;
begin
  len := Length(S);
  Result := (len >= 2) and (S[len - 1] = #13) and (S[len] = #10);
end;

procedure TruncateCRLF(var S: string);
var
  I: Integer;
begin
  I := 1;
  while I <= Length(S) do
    if (S[I] = #13) or (S[I] = #10) then Delete(S, I, 1)
    else Inc(I);
end;

function ReplaceString(var S: string; const Token, NewToken: string; bCaseSensitive: Boolean): Boolean;
var
  I         : Integer;
  sFirstPart: string; 
begin
  if bCaseSensitive then
    I := AnsiPos(Token, S)
  else
    I := AnsiPos(AnsiUpperCase(Token), AnsiUpperCase(S));
  
  if I <> 0 then
  begin
    sFirstPart := Copy(S, 1, I - 1) + NewToken; // 磷

⌨️ 快捷键说明

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