Jump to content
toufik

Extract string between two number delphi

Recommended Posts

hello every one ,,

i'm trying to extract  a string g lets say i have this long string :


2090Lj32J5Y6Ni6Q2Rt8c538u1X227L340Q8N1....ce4A3z6l96S5v7LZ3OhSVoB538k7wPTn13E2w2D0h104fJ7HP09x

o7W71J1o02c088922A3t420697F5431XP91C6JF9w1Ss0Tuk5S669207816gp57pW193BPWL27AZ052nr2S714...30

ps  : i added the point(....) to show the palaces i want  to split the string.

i want tto be able  to get start from 3 string and put it  in variable to use it later 

2090Lj32J5Y6Ni6Q2Rt8c538u1X227L340Q8N1 
and .......

ce4A3z6l96S5v7LZ3OhSVoB538k7wPTn13E2w2D0h104fJ7HP09x

o7W71J1o02c088922A3t420697F5431XP91C6JF9w1Ss0Tuk5S669207816gp57pW193BPWL27AZ052nr2S714v
and ......

30

..
i  did  tried to play with this ;  Registration_number :=(length(str)).ToString; 
no luck yet .

Share this post


Link to post

One possible solution:

procedure TForm1.Button1Click(Sender: TObject);
const
    SrcString : String = '2090Lj32J5Y6Ni6Q2Rt8c538u1X227L340Q8N1...ce4A3z6l96S5v7LZ3OhSVoB538k7wPTn13E2w2D0h104fJ7HP09xo7W71J1o02c088922A3t420697F5431XP91C6JF9w1Ss0Tuk5S669207816gp57pW193BPWL27AZ052nr2S714...30';
    Delim     : String = '...';
var
    S : array [1..3] of String;
    I, J, K    : Integer;
begin
    J := 1;
    I := Low(S);
    while I <= High(S) do begin
        K := Pos(Delim, SrcString, J);
        if K <= 0 then begin
            S[I] := Copy(SrcString, J);
            while I < High(S) do begin
                Inc(I);
                S[I] := '';
            end;
            break;
        end;
        S[I] := Copy(SrcString, J, K - J);
        J    := K + Length(Delim);
        Inc(I);
    end;

    for I := Low(S) to High(S) do
        Memo1.Lines.Add(S[I]);
end;

 

Share this post


Link to post

You can use TStringHelper.Split.

procedure TForm2.Button1Click(Sender: TObject);
const
    SrcString : String = '2090Lj32J5Y6Ni6Q2Rt8c538u1X227L340Q8N1...ce4A3z6l96S5v7LZ3OhSVoB538k7wPTn13E2w2D0h104fJ7HP09xo7W71J1o02c088922A3t420697F5431XP91C6JF9w1Ss0Tuk5S669207816gp57pW193BPWL27AZ052nr2S714...30';
    Delim     : String = '...';
var
    S : TArray<string>;
begin
  S := SrcString.Split(Delim);
  Memo1.Lines.AddStrings(S);
end;

 

  • Like 1

Share this post


Link to post

so the dots are not part of the string.

do you know the length of the breaks? or a pattern to look for? seems pretty random.

and where did the "14v" <- the v not in the original string..

Share this post


Link to post
Guest

more easy...  helper for STRING type = SPLIT(...)

 

var

MyArrStrings:TArray<string>;

begin

  MyArrStrings := MyStrWithValuesToSplit.SPLIT(['...']);

  ....

MyArrStrings[0] .... MyArrStrings[n]

 

  for var MyItem in MyArrStrings do

ShowMessage( MyItem )

 

....using   JOIN(...) you rebuilt it if you need reverse task

 

 

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×