Parsi Coders

نسخه‌ی کامل: کار با رشته ها در دلفی 2010
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام
تو دلفی 2010 میخواستم از تابع leftstr rightstr و ... استفاده منم با مشکل برخوردم فهمیدم که باید در قسمت use
کد:
strutils
رو تعریف کنید یه مثال میزنم با تابع rightstr
کد:
unit Unit1;
//coder: amin mansouri
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,strutils,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
s : string;
begin
s := 'ABOUT DELPHI PROGRAMMING www.parsicoders.com';
s := rightStr(s,20);
showmessage(s)
end;

end.
اینم تابع instr
کد:
fuction InStr(sIni: string; sSubStr: string): Boolean;


var

   iPos: integer;

begin


  iPos := Pos(sSubStr, sIni);
  If iPos <> 0 Then


    Result := True

  Else

    Result := False

end;
برای فراخوانی تابع هم باید اینجوری عمل کنید
کد:
{
    Example Usage:
    
    procedure Example();
    var
        test: string;
    begin
        test := 'ip lorem sum';
        If InStr(test, 'lorem') Then ShowMessage('String found in identifier test');
    end;
    
    Example();
}
این هم توابع vb6 برای کار با stringha تبدیل شده به دلفی
کد:
                
!**************************************
! Name: Replace,InStr,Left,Right,LeftTrim,RightTrim,Reverse,Split,Replace,Mid Functions
! Description:This is just like the name says, The Following Vb functions for Delphi all coded/translated by me:
RightTrim
LeftTrem
InStr
Mid
Left
Right
Replace
Split
Reverse
! By: Tanner Ezell (aka Lord Nova Ice)
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=382&lngWId=7!for details.!**************************************

unit Functions;
interface
uses
Windows,Messages, SysUtils, Classes, Graphics,StdCtrls;
function RightTrim(const s:String):String;
function LeftTrim(const s:String):String;
function InStr(Start: integer; Source: string; SourceToFind: string): integer;
function Mid(Source: string; Start: integer; Length: integer): string;
function Left(Source: string; Length: integer): string;
function Right(Source: string; Lengths: integer): string;
function Replace(sData: String; sSubstring: String; sNewsubstring: string): String;
function Split(Source, Deli: string; StringList: TStringList): TStringList;
function Reverse(Line: string): string;
implementation
function Reverse(Line: string): string;
    var i: integer;
    var a: string;
begin
    For i := 1 To Length(Line) do
    begin
    a := Right(Line, i);
    Result := Result + Left(a, 1);
    end;
end;
function Split(Source, Deli: string; StringList: TStringList): TStringList;
var
EndOfCurrentString: byte;
begin
repeat
EndOfCurrentString := Pos(Deli, Source);
if EndOfCurrentString = 0 then
StringList.add(Source)
else
StringList.add(Copy(Source, 1, EndOfCurrentString - 1));
Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);
until EndOfCurrentString = 0;
result := StringList;
end;
function Replace(sData: String; sSubstring: String; sNewsubstring: string): String;
var
i: integer;
lSub: Longint;
lData: Longint;
begin
i := 1;
lSub := Length(sSubstring);
lData := Length(sData);
repeat
begin
i := InStr(i, sData, sSubstring);
If i = 0 Then
begin
sNewSubString := sData;
Exit
end
Else
sData := Copy(sData, 1, i - 1) + sNewsubstring + Copy(sData, i + lSub, lData);
i := i + lSub;
End;
Until i > lData;
Replace := sData;
end;
function Left(Source: string; Length: integer): string;
begin
    Result := copy(Source,1,Length);
end;
function Right(Source: string; Lengths: integer): string;
begin
Result := copy(source,Length(Source) - Lengths,Lengths);
end;
function Mid(Source: string; Start: integer; Length: integer): string;
begin
    Result := copy(Source,Start,Length);
end;
function InStr(Start: integer; Source: string; SourceToFind: string): integer;
begin
    Result := pos(SourceToFind,copy(Source,Start,Length(Source) - (Start - 1)));
end;
function RightTrim(const s:String):String;
var
i:integer;
begin
i:=length(s);
while (i>0) and (s[i]<=#32) do
Dec(i);
result:=Copy(s,1,i);
end;
function LeftTrim(const s:String):String;
var
i, L:integer;
begin
L:=length(s);
i:=1;
while (i<=L) and (s[i]<=#32) do
Inc(i);
result:=Copy(s,i, MaxInt);
end;
end.
mamnoon