Recent

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

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Is there a function called MidStr??
« Reply #15 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)
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

dsiders

  • Hero Member
  • *****
  • Posts: 1439
Re: Is there a function called MidStr??
« Reply #16 on: January 30, 2023, 12:38:37 am »
You have function Copy: **s://www.freepascal.org/docs-html/rtl/system/copy.html

And UTF8Copy: **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
« Last Edit: January 30, 2023, 12:43:05 am by dsiders »
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

OC DelGuy

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

Thaddy

  • Hero Member
  • *****
  • Posts: 17187
  • Ceterum censeo Trump esse delendam
Re: Is there a function called MidStr??
« Reply #18 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.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Is there a function called MidStr??
« Reply #19 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-)
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

CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Is there a function called MidStr??
« Reply #20 on: May 23, 2025, 01:55:24 pm »
Since I started using Lazarus I am wondering why the ACount parameter of MidStr is not optional.
And just today I figured out, that I shall use it this was
Code: Pascal  [Select][+][-]
  1. Midstr('afsafsdafasdfasfdas',3,MaxInt);
This still does not explain why ACount is not optional.
« Last Edit: May 23, 2025, 01:56:57 pm by CM630 »
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Is there a function called MidStr??
« Reply #21 on: May 23, 2025, 02:23:41 pm »
Since I started using Lazarus I am wondering why the ACount parameter of MidStr is not optional.
Reason + backwards compatibility.

Alternative but could just as well opt for a custom midstr function.
Today is tomorrow's yesterday.

Awkward

  • Full Member
  • ***
  • Posts: 150
Re: Is there a function called MidStr??
« Reply #22 on: May 23, 2025, 02:48:28 pm »
Why do not to use Copy(string,start index[, count]) function?

CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Is there a function called MidStr??
« Reply #23 on: May 23, 2025, 08:25:01 pm »
Why do not to use Copy(string,start index[, count]) function?
According this info https://www.freepascal.org/docs-html/rtl/system/copy.html the Count parameter of Copy is not optional. So it is quite the same.
Copy takes a String as a parameter, while MidStr takes a Widestring, maybe this might result in some problems.

Actually, none of these works:
Code: Pascal  [Select][+][-]
  1.   Memo1.Append(MidStr('юяㅓ ㅕцчш ㅋ ㅌ',3,MaxInt));
  2.   Memo1.Append(Copy('юяㅓ ㅕцчш ㅋ ㅌ',3,MaxInt));
  3.   Memo1.Append('юяㅓ ㅕцчш ㅋ ㅌ'.Substring(3));

Here is the result:
Quote
яㅓ ㅕцчш ㅋ ㅌ
яㅓ ㅕцчш ㅋ ㅌ
?ㅓ ㅕцчш ㅋ ㅌ


This seems fine:

Quote
Memo1.Append(UTF8Copy('юяㅓ ㅕцчш ㅋ ㅌ',3,maxint)); 
« Last Edit: May 23, 2025, 08:28:37 pm by CM630 »
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

d2010

  • Full Member
  • ***
  • Posts: 171
Re: Is there a function called MidStr??
« Reply #24 on: May 23, 2025, 09:24:46 pm »
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.  

Here my Code is good
Code: Pascal  [Select][+][-]
  1. Function Vlax_get_IndexStr(const AText: string; const AValues: array of string): Integer;
  2. var
  3.   i : Integer;
  4. begin
  5.   Result:=RTCAn;
  6.   if (high(AValues)=-1) or (High(AValues)>MaxInt) Then Exit;
  7.   for i:=low(AValues) to High(Avalues) do
  8.      if CompareText(avalues[i],atext)=0 Then
  9.       begin Result:=i; break;end;
  10. end;
  11.  
  12. Function Ansi_get_Random(const AValues: array of string): string;
  13. begin
  14.   if (high(AValues)=-1) or (High(AValues)>kMaxInt) Then result:='' else
  15.      result:=Avalues[random(High(AValues)+1)];
  16. end;
  17.  
  18. Function vasi_get_MidStr(const AText: String; const AStart, ACount: integer): String;
  19. begin
  20.   if (ACount<1) or (AStart>length(atext)) then result:='' else
  21.        Result:=Copy(AText,AStart,ACount);
  22. end;
  23.  
  24. Function vasi_idx_MidStr(const AText: String; AStart, EStart: integer): String;
  25. Var dwg:integer;
  26. begin
  27.    if (aStart>eStart)  and (eStart>00) then  Begin dwg:=astart; aStart:=EStart; eStart:=dwg; End;
  28.    if (estart>length(Atext)) then estart:=length(Atext);
  29.   if (aStart<1) or (AStart>length(atext)) then result:='' else
  30.        Result:=Copy(AText,AStart,Estart-Astart+1);
  31. end;
  32.  
« Last Edit: May 23, 2025, 09:38:36 pm by d2010 »

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Is there a function called MidStr??
« Reply #25 on: May 24, 2025, 12:03:14 am »
This still does not explain why ACount is not optional.
Design choices cannot always be explained. They're simply choices. The beauty is that it's easy to write your own custom function if mandatory ACount annoys you. Here's one that's reasonably fast:

Code: Pascal  [Select][+][-]
  1. function MidStr2(const ASource: AnsiString; AStart: Integer; ACount: Integer = -1): AnsiString;
  2. var
  3.   SrcLen: Integer;
  4.   PSrc, PDest: PChar;
  5.   Count: Integer;
  6. begin
  7.   SrcLen := Length(ASource);
  8.   if (AStart < 1) or (AStart > SrcLen) then
  9.     begin
  10.       Result := '';
  11.       Exit;
  12.     end;
  13.  
  14.   if ACount = -1 then
  15.     Count := SrcLen - AStart + 1
  16.   else if ACount < 0 then
  17.     begin
  18.       Result := '';
  19.       Exit;
  20.     end
  21.   else
  22.     Count := ACount;
  23.  
  24.   if AStart + Count - 1 > SrcLen then
  25.     Count := SrcLen - AStart + 1;
  26.  
  27.   SetLength(Result, Count);
  28.   PSrc := PChar(ASource) + AStart - 1;
  29.   PDest := PChar(Result);
  30.   Move(PSrc^, PDest^, Count * SizeOf(Char));
  31. end;
It's only logical.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6004
  • Compiler Developer
Re: Is there a function called MidStr??
« Reply #26 on: May 25, 2025, 09:45:21 pm »
Why do not to use Copy(string,start index[, count]) function?
According this info https://www.freepascal.org/docs-html/rtl/system/copy.html the Count parameter of Copy is not optional. So it is quite the same.

When you link the documentation, maybe you should also read it completely?

Quote from: Documentation
The Count argument can be omitted. In that case, the string (or dynamic array) is copied from the position Index till the end of the string or array.

jamie

  • Hero Member
  • *****
  • Posts: 6960
Re: Is there a function called MidStr??
« Reply #27 on: May 26, 2025, 12:48:23 pm »
I have good luck using the utf8decode over to a uni string Unicode string that is and do the copy function and I can convert it back afterwards
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018