Lazarus

Programming => General => Topic started by: OC DelGuy on January 27, 2023, 05:12:18 am

Title: Is there a function called MidStr??
Post by: OC DelGuy on January 27, 2023, 05:12:18 am
I looked at the reference doc for FPC and see RightStr and LeftStr.  They both return characters from the end and the beginning , respectively.  Is there a MidStr or something that extracts characters from the string in between?

Code: Pascal  [Select][+][-]
  1. Saying:='Impossible is for the unwilling';
  2. WriteLn(LeftStr(Saying,10);
  3. WriteLn(RightStr(Saying,9);
  4.  

Output:
Code: Text  [Select][+][-]
  1. Impossible
  2. unwilling
  3.  

I'm looking for something like this:
Code: Pascal  [Select][+][-]
  1. WriteLn(MidStr(saying,15,3));

Output:
Code: Text  [Select][+][-]
  1. for

Title: Re: Is there a function called MidStr??
Post by: dseligo on January 27, 2023, 05:33:27 am
You have function Copy: https://www.freepascal.org/docs-html/rtl/system/copy.html (https://www.freepascal.org/docs-html/rtl/system/copy.html)

And UTF8Copy: https://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html (https://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html)
Title: Re: Is there a function called MidStr??
Post by: Bogen85 on January 27, 2023, 05:33:40 am
Yes.
It is copy
https://www.freepascal.org/docs-html/rtl/system/copy.html


Code: Pascal  [Select][+][-]
  1. program midstring;
  2. {$mode objfpc}
  3. {$h+}
  4.  
  5. var
  6.   s: string;
  7.   midStr: string;
  8. begin
  9.   s := 'That is a copy!';
  10.   midStr := Copy(s, 6, 4); // midStr will be 'is a'
  11.   writeln(midStr);
  12. end.
Title: Re: Is there a function called MidStr??
Post by: OC DelGuy on January 27, 2023, 06:46:25 am
Yes.
It is copy
https://www.freepascal.org/docs-html/rtl/system/copy.html

Right-o Bogen!  Thanks!
Title: Re: Is there a function called MidStr??
Post by: TRon on January 27, 2023, 06:48:32 am
or: https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.substring.html
Title: Re: Is there a function called MidStr??
Post by: Bogen85 on January 27, 2023, 11:07:53 am
And UTF8Copy: https://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html (https://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html)

This!  ;D

Is likely the preferred overall.
Title: Re: Is there a function called MidStr??
Post by: Thaddy on January 27, 2023, 11:35:05 am
Other midstr option (complete program) using type helper for AnsiString:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;
  3. begin
  4.   writeln('Impossible is for the unwilling'.substring(14,3));  // prints for
  5. end.
Title: Re: Is there a function called MidStr??
Post by: Bogen85 on January 27, 2023, 11:40:34 am
or: https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.substring.html

This is not UTF-8 aware though.

Code: Pascal  [Select][+][-]
  1. program midstringhelp;
  2. {$mode objfpc}
  3. {$h+}
  4. {$codepage utf8}
  5.  
  6. uses SysUtils, StrUtils;
  7.  
  8. var
  9.   s, midStr: string;
  10. begin
  11.   s := 'þháþ íß á ©óöü!';
  12.   midStr := s.Substring(6, 4); // midStr should be 'íß á'
  13.   writeln(midStr);
  14. end.

The expected 4 "characters" (not bytes) at "character" index (not byte) index is not what is returned.
Title: Re: Is there a function called MidStr??
Post by: Thaddy on January 27, 2023, 11:42:52 am
True, but not for long: there is much work being done for a unicode16 based rtl.

It is already in the unicodertl branch.
https://gitlab.com/freepascal.org/fpc/source/-/blob/unicodertl/rtl/objpas/sysutils/syshelph.inc

Either way, to OP it does not matter because in his question is not about UTF8.
Title: Re: Is there a function called MidStr??
Post by: marcov on January 27, 2023, 12:23:29 pm
Or simply use midstr ?  ;D

https://www.freepascal.org/docs-html/rtl/strutils/midstr.html

afaik it was added as a wrapper around copy for VB migration purposes in D6 or so.
Title: Re: Is there a function called MidStr??
Post by: Thaddy on January 27, 2023, 12:33:48 pm
Oh, forgot all about that. It was added to Delphi even earlier than D6, I believe.
Title: Re: Is there a function called MidStr??
Post by: marcov on January 27, 2023, 12:35:50 pm
Possible, but only D6 added strutils, so then it must have moved from elsewhere.
Title: Re: Is there a function called MidStr??
Post by: Thaddy on January 27, 2023, 12:47:03 pm
Possible, but only D6 added strutils, so then it must have moved from elsewhere.
Or we wrote one ourselves, memory leak, but about strutils introduced in D6 you are right. It is not in D4.
Title: Re: Is there a function called MidStr??
Post by: PascalDragon on January 27, 2023, 04:45:21 pm
True, but not for long: there is much work being done for a unicode16 based rtl.

Even with a UTF-16-based RTL one needs to be careful not to cut a surrogate pair. Or characters that consist of multiple code points (e.g. a¨ for a normalized ä) - a problem which would also exist with UTF-32. Also having the UTF-16 RTL available will not mean that all targets will switch to that. Some might stay ANSI/UTF-8 (e.g. for *nix systems the ANSI/UTF-8 RTL would mean fewer conversions while for Windows the UTF-16 RTL means fewer conversions).
Title: Re: Is there a function called MidStr??
Post by: OC DelGuy on January 30, 2023, 12:25:26 am
You have function Copy: **s://www.freepascal.org/docs-html/rtl/system/copy.html (http://**s://www.freepascal.org/docs-html/rtl/system/copy.html)

And UTF8Copy: **s://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html (http://**s://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html)
These two links don't seem to work...
Title: Re: Is there a function called MidStr??
Post by: OC DelGuy on January 30, 2023, 12:30:42 am
Other midstr option (complete program) using type helper for AnsiString:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;
  3. begin
  4.   writeln('Impossible is for the unwilling'.substring(14,3));  // prints for
  5. end.

Can you assign this to a Variable?

Variable:='Impossible is for the unwilling'.substring(14,3)
Title: Re: Is there a function called MidStr??
Post by: dsiders on January 30, 2023, 12:38:37 am
You have function Copy: **s://www.freepascal.org/docs-html/rtl/system/copy.html (http://**://**://**://**://**://**s://www.freepascal.org/docs-html/rtl/system/copy.html)

And UTF8Copy: **s://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html (http://**://**://**://**://**://**s://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html)
These two links don't seem to work...

The forum software seems to be borking the urls. Replace ** with h-t-t-p (no dashes).

Code: Text  [Select][+][-]
  1. https://www.freepascal.org/docs-html/rtl/system/copy.html
  2. https://lazarus-ccr.sourceforge.io/docs/lazutils/lazutf8/utf8copy.html
Title: Re: Is there a function called MidStr??
Post by: OC DelGuy on January 30, 2023, 05:21:44 am
The forum software seems to be borking the urls. Replace ** with h-t-t-p (no dashes).
Understood.
Title: Re: Is there a function called MidStr??
Post by: Thaddy on January 30, 2023, 08:48:06 am
Can you assign this to a Variable?

Variable:='Impossible is for the unwilling'.substring(14,3)
Yes. But not on declaration, since substring is a function that is not evaluated at declaration time. But it will if assigned when in the program or procedure body.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;
  3. var s:string; // this is not possible here: = 'Impossible is for the unwilling'.substring(14,3);
  4. begin
  5.   s:= 'Impossible is for the unwilling'.substring(14,3);  // prints for
  6.   writeln(s);
  7. end.
Title: Re: Is there a function called MidStr??
Post by: OC DelGuy on January 31, 2023, 01:55:20 am
Can you assign this to a Variable?

Variable:='Impossible is for the unwilling'.substring(14,3)
Yes. But not on declaration, since substring is a function that is not evaluated at declaration time. But it will if assigned when in the program or procedure body.
Got it.  8-)
TinyPortal © 2005-2018