Topic: SubString (Mid) Function

Does MVD support a subString, mid, extract  type of function to get a piece of an existing string?
Also Left() or Right() functions?

Keith

2 (edited by derek 2020-12-14 13:59:50)

Re: SubString (Mid) Function

Hi Keith,
A few ways to do it.
I tend to use 'copy' (either copying the result to another field (as in the attached example) or back to itself);  you can do 'left', 'mid' and 'right' all at the same time.  Also in the attachment is a screenshot within the script editor where you can click on the 'functions' button to get a list of what's available.
Derek.

Post's attachments

Attachment icon substrings.zip 508.97 kb, 236 downloads since 2020-12-14 

Re: SubString (Mid) Function

Derek,

Thanx for the reply and info... while I was looking at the Function List I just never equated "copy" at being the function I wanted !!

While you might not like this style of programming, my example, can you or anyone explain the oddity here.

//days := 'Sunday   Monday   Tuesday  WednesdayThursday Friday   Saturday ';

days := ''   + format('%9s',['Sunday']) +  format('%9s',['Monday']);
days := days + format('%9s',['Tuesday']) + format('%9s',['Wednesday']);
days := days + format('%9s',['Thursday']) +format('%9s',['Friday']);
days := days + format('%9s',['Saturday']);

ShowMessage('Days String =>' + days +'<=' + IntToStr(Length(days)));
showMessage('Day 5 is Thurs...' +copy(days,((5 * 9)-9)+1,9) +' OR '+
            copy(days,((5 * 9)-9)+1,3));

if you un-comment the first sting (days) this works fine,
however using the format() option the first copy in the final ShowMessage works but the second
one is off by one char.  Even adding a Trim() to the loading of Wednesday does not fix the issue.
Is there something I am missing with either the Copy() or the format() functions?

Regards,
Keith

4 (edited by derek 2020-12-14 21:18:39)

Re: SubString (Mid) Function

Hi Keith,
Your 'copy' statement is fine.
Try changing your 'format' statement as follows:
days := ''   + format('%-9s',['Sunday']) +  format('%-9s',['Monday']);
days := days + format('%-9s',['Tuesday']) + format('%-9s',['Wednesday']);
days := days + format('%-9s',['Thursday']) +format('%-9s',['Friday']);
days := days + format('%-9s',['Saturday']);
So basically it's '%-9s' throughout.
I think that should fix it.
Derek.

Re: SubString (Mid) Function

Yes Sir, that does indeed fix things  !!

Regards,
Keith

6 (edited by jrga 2023-09-05 12:11:52)

Re: SubString (Mid) Function

functions created from the substrings project written by Derek, above


//=================================================================
function left_mid_str(word:string;pos_ini:integer;len_sub:integer): string;
begin
    result := copy(word,pos_ini,len_sub);
end;
//=================================================================
function rigth_str(word:string;len_sub:integer): string;
begin
    result := copy(word,(length(word)-len_sub+1),len_sub);
end;
//=================================================================
procedure Form1_OnShow (Sender: TObject; Action: string);
begin
    showmessage(left_mid_str('1234567890',1,3));
    showmessage(rigth_str('1234567890',3));
end;
Roberto Alencar