Archiv für Strings
Check Text
Samstag, 12. Mai 2007begin
If not (Key in [‘0′..‘9′,‘.’,#8, #13]) then
Key := #0;
If Key = ‘.’ then
begin
If Pos(‘.’, Sender.Text) > 0 then
Key := #0;
end;
end;
[tags]Delphi, Strings[/tags]
Encrypt & DeCrypt
Samstag, 12. Mai 2007var
CharIndex : Integer;
ReturnValue : String;
begin
ReturnValue := ”;
for CharIndex := 1 to Length(Value) do
begin
ReturnValue := ReturnValue + chr(NOT(ord(Value[CharIndex])));
end;
Result := ReturnValue;
end;
[tags]Delphi, Strings[/tags]
Fix Path
Samstag, 12. Mai 2007// Appends a back slash to a path if needed
If Path[length(Path)]<>‘\’ then Path := Path +’\';
[tags]Delphi, Strings[/tags]
Formatting
Samstag, 12. Mai 2007Format(‘%-35s’,[MyString])
// Where 35 is the length to make the string
// And MyString is the string to format
Formatting numbers :
I : Integer;
S : String;
begin
I := StrToInt(Edit1.Text); // Get a number
FmtStr(S, ‘%.3d’, [I]);
Edit2.Text := S; // write out the string representation of it.
// I.E. If Edit1.Text := ‘1′ Then Edit2.Text := ‘001′;
end;
[tags]Delphi, Strings[/tags]
Get Parsed item
Samstag, 12. Mai 2007Var
I4 : Integer;
S4 : String;
Begin
TheItemStr := TheItemStr + ParseStr;
S4 := ”;
I4 := 0;
While I4 <= ItemNum Do
Begin
S4 := Copy(TheItemStr, 1, Pos(ParseStr, TheItemStr) - 1);
Delete(TheItemStr, 1, Pos(ParseStr, TheItemStr));
Inc(I4);
End;
Result := S4;
End;
[tags]Delphi, Strings[/tags]
How to use “stringreplace”
Samstag, 12. Mai 2007>could you tell me how to use “stringreplace”
>and if you don’t mind please send me a sample code
Are you having a specific problem with StringReplace?
From the help:
>>>>
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string; Flags:
TReplaceFlags): string;
< <<<
Examples:
weiterlesen »
Random String
Samstag, 12. Mai 2007LoopInt : Integer;
DirName, FullName, RanStr : String;
FileSavedTo : TextFile;
// Length of string to create
RandArray : Array[0..4087] Of Char;
FirstCount : Extended;
begin
FirstCount := GetTickCount;
Label2.Caption := ”;
Randomize;
RanStr := ”;
DirName := Directory95ListBox1.Directory;
If DirName[Length(DirName)] <> ‘\’ Then DirName := DirName + ‘\’;
FullName := DirName + Edit1.Text;
If FileExists(FullName) Then DeleteFile(FullName);
For LoopInt := Low(RandArray) To High(RandArray) Do
Begin
RanStr := RanStr + Chr(Random(255 - 32 + 1) + 32);
End;
AssignFile(FileSavedTo, FullName);
If FileExists(FullName) Then
Reset(FileSavedTo)
Else
Rewrite(FileSavedTo);
Writeln(FileSavedTo, RanStr);
CloseFile(FileSavedTo);
Label2.Caption := ‘DONE’;
Label4.Caption := FloatToStr((GetTickCount - FirstCount) / 1000);
FileListBox1.Update;
[tags]Delphi, Strings[/tags]
Strip Comments from String
Samstag, 12. Mai 2007STRIP_STATES = ( SS_NORMA, SS_BRACE, SS_PAREN, SS_LITER );
function SCFS( const S : string; var SS : STRIP_STATES ): string;
// strip comments from string
var
I : integer;
begin
Result := ”;
I := 1;
while I <= Length( S ) do begin
case SS of
SS_NORMA :
case S[ I ] of
‘{‘ : if ( I < Length( S ) ) and ( S[ I + 1 ] = ‘$’ )
then Result := Result + S[ I ]
else SS := SS_BRACE;
‘(‘ : if ( I < Length( S ) ) and ( S[ I + 1 ] = ‘*’ )
then begin
if ( I < Length( S ) - 1 ) and ( S[ I + 2 ] = ‘$’ )
then Result := Result + S[ I ]
else begin SS := SS_PAREN; Inc( I ); end;
end else Result := Result + S[ I ];
‘/’ : if ( I < Length( S ) ) and ( S[ I + 1 ] = ‘/’ )
then Exit
else Result := Result + S[ I ];
”” : begin SS := SS_LITER; Result := Result + S[ I ]; end;
else Result := Result + S[ I ];
end;
SS_BRACE :
case S[ I ] of
‘}’ : SS := SS_NORMA;
end;
SS_PAREN :
case S[ I ] of
‘*’ : if ( I < Length( S ) ) and ( S[ I + 1 ] = ‘)’ )
then begin SS := SS_NORMA; Inc( I ); end;
end;
SS_LITER :
case S[ I ] of
”” : begin SS := SS_NORMA; Result := Result + S[ I ]; end;
else Result := Result + S[ I ];
end;
end; // case
SS of
Inc( I );
end; // while I <= Length( S ) do begin
end; // SCFS
procedure STRIP_COMMENTS( slIN, slOUT : TStringList;
EMPTY_LINES : boolean = false; // True – to preserve empty lines
LEFT_TRIM : boolean = false; // True – to strip indentation
RIGHT_TRIM : boolean = true ); // False – to preserve right white spaces
// strips Object Pascal’s comments
// slIN – input, slOUT – output
var
I : integer;
STRIP_STATE : STRIP_STATES;
S : string;
begin
STRIP_STATE := SS_NORMA;
for I := 0 to slIN.Count - 1 do begin
S := SCFS( slIN[ I ], STRIP_STATE );
if LEFT_TRIM then S := TrimLeft( S );
if RIGHT_TRIM then S := TrimRight( S );
if EMPTY_LINES or ( Length( Trim( S )) > 0 ) then slOUT.Append( S );
end;
end; // STRIP_COMMENTS
[tags]Delphi, Strings[/tags]
Tokenizing
Samstag, 12. Mai 2007Tokenize a string:
ExtractString (Classes.pas)
[tags]Delphi, Strings[/tags]