Recent

Author Topic: Is there a function called MidStr??  (Read 2364 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Is there a function called MidStr??
« 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

« Last Edit: January 27, 2023, 05:14:40 am by OC DelGuy »
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dseligo

  • Hero Member
  • *****
  • Posts: 1196
« Last Edit: January 30, 2023, 12:44:58 am by dseligo »

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Is there a function called MidStr??
« Reply #2 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.

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 2435

Bogen85

  • Hero Member
  • *****
  • Posts: 595

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Is there a function called MidStr??
« Reply #6 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.
« Last Edit: January 27, 2023, 11:41:44 am by Thaddy »
Specialize a type, not a var.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Is there a function called MidStr??
« Reply #7 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Is there a function called MidStr??
« Reply #8 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.
« Last Edit: January 27, 2023, 12:15:41 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Is there a function called MidStr??
« Reply #9 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Is there a function called MidStr??
« Reply #10 on: January 27, 2023, 12:33:48 pm »
Oh, forgot all about that. It was added to Delphi even earlier than D6, I believe.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Is there a function called MidStr??
« Reply #11 on: January 27, 2023, 12:35:50 pm »
Possible, but only D6 added strutils, so then it must have moved from elsewhere.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Is there a function called MidStr??
« Reply #12 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.
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Is there a function called MidStr??
« Reply #13 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).

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

 

TinyPortal © 2005-2018