📄 ststrw.pas
字号:
if (P1 > 1) and (pos(TmpStr[P1-1], WordDelims) > 0) then
Inc(I);
end;
if (I = N) then begin
Result := True;
Position := Position + P1;
Exit;
end;
System.Delete(TmpStr, 1, P2);
Position := Position + P2;
P1 := pos(AWord, TmpStr);
end;
end;
function CopyFromNthWordW(const S, WordDelims : WideString;
AWord : WideString; N : Cardinal;
var SubString : WideString) : Boolean;
var
P : Cardinal;
begin
if (WordPosW(S, WordDelims, AWord, N, P)) then begin
SubString := Copy(S, P, Length(S));
Result := True;
end else begin
SubString := '';
Result := False;
end;
end;
function DeleteFromNthWordW(const S, WordDelims : WideString;
AWord : WideString; N : Cardinal;
var SubString : WideString) : Boolean;
var
P : Cardinal;
begin
SubString := S;
if (WordPosW(S, WordDelims, AWord, N, P)) then begin
Result := True;
SubString := Copy(S, 1, P-1);
end else begin
Result := False;
SubString := '';
end;
end;
function CopyFromToWordW(const S, WordDelims, Word1, Word2 : WideString;
N1, N2 : Cardinal;
var SubString : WideString) : Boolean;
var
P1,
P2 : Cardinal;
begin
if (WordPosW(S, WordDelims, Word1, N1, P1)) then begin
if (WordPosW(S, WordDelims, Word2, N2, P2)) then begin
Dec(P2);
if (P2 > P1) then begin
Result := True;
SubString := Copy(S, P1, P2-P1);
end else begin
Result := False;
SubString := '';
end;
end else begin
Result := False;
SubString := '';
end;
end else begin
Result := False;
SubString := '';
end;
end;
function DeleteFromToWordW(const S, WordDelims, Word1, Word2 : WideString;
N1, N2 : Cardinal;
var SubString : WideString) : Boolean;
var
P1,
P2 : Cardinal;
begin
SubString := S;
if (WordPosW(S, WordDelims, Word1, N1, P1)) then begin
if (WordPosW(S, WordDelims, Word2, N2, P2)) then begin
Dec(P2);
if (P2 > P1) then begin
Result := True;
System.Delete(SubString, P1, P2-P1+1);
end else begin
Result := False;
SubString := '';
end;
end else begin
Result := False;
SubString := '';
end;
end else begin
Result := False;
SubString := '';
end;
end;
function CopyWithinW(const S, Delimiter : WideString;
Strip : Boolean) : WideString;
var
P1,
P2 : Cardinal;
TmpStr : WideString;
begin
if (S = '') or (Delimiter = '') or (pos(Delimiter, S) = 0) then
Result := ''
else begin
if (StrStPosW(S, Delimiter, P1)) then begin
TmpStr := Copy(S, LongInt(P1) + Length(Delimiter), Length(S));
if StrStPosW(TmpStr, Delimiter, P2) then begin
Result := Copy(TmpStr, 1, P2-1);
if (not Strip) then
Result := Delimiter + Result + Delimiter;
end else begin
Result := TmpStr;
if (not Strip) then
Result := Delimiter + Result;
end;
end;
end;
end;
function DeleteWithinW(const S, Delimiter : WideString) : WideString;
var
P1,
P2 : Cardinal;
TmpStr : WideString;
begin
if (S = '') or (Delimiter = '') or (pos(Delimiter, S) = 0) then
Result := ''
else begin
if (StrStPosW(S, Delimiter, P1)) then begin
TmpStr := Copy(S, LongInt(P1) + Length(Delimiter), Length(S));
if (pos(Delimiter, TmpStr) = 0) then
Result := Copy(S, 1, P1-1)
else begin
if (StrStPosW(TmpStr, Delimiter, P2)) then begin
P2 := LongInt(P2) + (2*Length(Delimiter));
Result := S;
System.Delete(Result, P1, P2);
end;
end;
end;
end;
end;
function ReplaceWordW(const S, WordDelims, OldWord, NewWord : WideString;
N : Cardinal;
var Replacements : Cardinal) : WideString;
var
I,
C,
P1 : Cardinal;
begin
if (S = '') or (WordDelims = '') or (OldWord = '') or
(pos(OldWord, S) = 0) then begin
Result := '';
Replacements := 0;
Exit;
end;
if (WordPosW(S, WordDelims, OldWord, N, P1)) then begin
Result := S;
System.Delete(Result, P1, Length(OldWord));
C := 0;
for I := 1 to Replacements do begin
if (((Length(NewWord)) + Length(Result)) < MaxLongInt) then begin
Inc(C);
System.Insert(NewWord, Result, P1);
Inc(P1, Length(NewWord) + 1);
end else begin
Replacements := C;
Exit;
end;
end;
end else begin
Result := S;
Replacements := 0;
end;
end;
function ReplaceWordAllW(const S, WordDelims, OldWord, NewWord : WideString;
var Replacements : Cardinal) : WideString;
var
I,
C,
P1 : Cardinal;
begin
if (S = '') or (WordDelims = '') or (OldWord = '') or
(Pos(OldWord, S) = 0) then begin
Result := S;
Replacements := 0;
end else begin
Result := S;
C := 0;
while (WordPosW(Result, WordDelims, OldWord, 1, P1)) do begin
System.Delete(Result, P1, Length(OldWord));
for I := 1 to Replacements do begin
if ((Length(NewWord) + Length(Result)) <= 255) then begin
Inc(C);
System.Insert(NewWord, Result, P1);
end else begin
Replacements := C;
Exit;
end;
end;
end;
Replacements := C;
end;
end;
function ReplaceStringW(const S, OldString, NewString : WideString;
N : Cardinal;
var Replacements : Cardinal) : WideString;
var
I,
C,
P1 : Cardinal;
TmpStr : WideString;
begin
if (S = '') or (OldString = '') or (pos(OldString, S) = 0) then begin
Result := S;
Replacements := 0;
Exit;
end;
TmpStr := S;
I := 1;
P1 := pos(OldString, TmpStr);
C := P1;
while (I < N) and (LongInt(C) < Length(TmpStr)) do begin
Inc(I);
System.Delete(TmpStr, 1, LongInt(P1) + Length(OldString));
Inc(C, LongInt(P1) + Length(OldString));
end;
Result := S;
System.Delete(Result, C, Length(OldString));
C := 0;
for I := 1 to Replacements do begin
if (((Length(NewString)) + Length(Result)) < MaxLongInt) then begin
Inc(C);
System.Insert(NewString, Result, P1);
Inc(P1, Length(NewString) + 1);
end else begin
Replacements := C;
Exit;
end;
end;
end;
function ReplaceStringAllW(const S, OldString, NewString : WideString;
var Replacements : Cardinal) : WideString;
var
I,
C : Cardinal;
P1 : longint;
Tmp: WideString;
begin
Result := S;
if (S = '') or (OldString = '') or (Pos(OldString, S) = 0) then
Replacements := 0
else begin
Tmp := S;
P1 := AnsiPos(OldString, S);
if (P1 > 0) then begin
Result := Copy(Tmp, 1, P1-1);
C := 0;
while (P1 > 0) do begin
for I := 1 to Replacements do begin
Inc(C);
Result := Result + NewString;
end;
Tmp := Copy(Tmp, P1+Length(OldString), MaxLongInt);
P1 := AnsiPos(OldString, Tmp);
if (P1 > 0) then begin
Result := Result + Copy(Tmp, 1, P1-1);
end else
Result := Result + Tmp;
end;
Replacements := C;
end else begin
Result := S;
Replacements := 0;
end;
end;
end;
function LastWordW(const S, WordDelims, AWord : WideString;
var Position : Cardinal) : Boolean;
var
TmpStr : WideString;
I : Cardinal;
begin
if (S = '') or (WordDelims = '') or
(AWord = '') or (pos(AWord, S) = 0) then begin
Result := False;
Position := 0;
Exit;
end;
TmpStr := S;
I := Length(TmpStr);
while (pos(TmpStr[I], WordDelims) > 0) do begin
System.Delete(TmpStr, I, 1);
I := Length(TmpStr);
end;
Position := Length(TmpStr);
repeat
while (pos(TmpStr[Position], WordDelims) = 0) and (Position > 1) do
Dec(Position);
if (Copy(TmpStr, Position + 1, Length(AWord)) = AWord) then begin
Inc(Position);
Result := True;
Exit;
end;
System.Delete(TmpStr, Position, Length(TmpStr));
Position := Length(TmpStr);
until (Length(TmpStr) = 0);
Result := False;
Position := 0;
end;
function LastWordAbsW(const S, WordDelims : WideString;
var Position : Cardinal) : Boolean;
begin
if (S = '') or (WordDelims = '') then begin
Result := False;
Position := 0;
Exit;
end;
{find first non-delimiter character, if any. If not a "one-word wonder"}
Position := Length(S);
while (Position > 0) and (pos(S[Position], WordDelims) > 0) do
Dec(Position);
if (Position = 0) then begin
Result := True;
Position := 1;
Exit;
end;
{find next delimiter character}
while (Position > 0) and (pos(S[Position], WordDelims) = 0) do
Dec(Position);
Inc(Position);
Result := True;
end;
function LastStringW(const S, AString : WideString;
var Position : Cardinal) : Boolean;
var
TmpStr : WideString;
I, C : Cardinal;
begin
if (S = '') or (AString = '') or (pos(AString, S) = 0) then begin
Result := False;
Position := 0;
Exit;
end;
TmpStr := S;
C := 0;
I := pos(AString, TmpStr);
while (I > 0) do begin
Inc(C, LongInt(I) + Length(AString));
System.Delete(TmpStr, 1, LongInt(I) + Length(AString));
I := pos(AString, TmpStr);
end;
{Go back the length of AString since the while loop deletes the last instance}
Dec(C, Length(AString));
Position := C;
Result := True;
end;
function KeepCharsW(const S, Chars : WideString) : WideString;
var
P1,
P2 : Cardinal;
begin
if (S = '') or (Chars = '') then begin
Result := '';
Exit;
end;
Result := '';
P1 := 1;
P2 := 1;
repeat
while (pos(S[P2], Chars) > 0) and (LongInt(P2) <= Length(S)) do
Inc(P2);
Result := Result + Copy(S, P1, P2-P1);
P1 := P2+1;
P2 := P1;
while (pos(S[P2], Chars) = 0) and (LongInt(P2) <= Length(S)) do
Inc(P2);
P1 := P2;
until (LongInt(P1) > Length(S));
end;
function RepeatStringW(const RepeatString : WideString;
var Repetitions : Cardinal;
MaxLen : Cardinal) : WideString;
var
J,
Len : Cardinal;
begin
Len := Length(RepeatString);
Repetitions := MaxLen div Len;
SetLength(Result, Repetitions * Len);
for J := 0 to pred(Repetitions) do
Move(RepeatString[1], Result[J * Len + 1], Len*SizeOf(WideChar));
end;
function TrimCharsW(const S, Chars : WideString) : WideString;
begin
Result := RightTrimCharsW(S, Chars);
Result := LeftTrimCharsW(Result, Chars);
end;
function RightTrimCharsW(const S, Chars : WideString) : WideString;
b
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -