Recent

Author Topic: How do i shrink the length of a string variable ?  (Read 3871 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 289
How do i shrink the length of a string variable ?
« on: August 22, 2023, 07:42:48 pm »
How do i shrink the length of a string variable ?

PortStr : String[3]

Does not seem to work, somebody how to go for a string of 3 characters?

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: How do i shrink the length of a string variable ?
« Reply #1 on: August 22, 2023, 07:52:46 pm »
What does not work?

String[3] is a shortstring type (old dos/turbo pascal type).
You define the length there and you can't change it.

If you want a dynamic string you just use String.
You can change the length then with Setlength.

What exactly do you want and how are you going to use it?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How do i shrink the length of a string variable ?
« Reply #2 on: August 22, 2023, 07:55:39 pm »
array[0..2] of char / ansichar / widechar ?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: How do i shrink the length of a string variable ?
« Reply #3 on: August 22, 2023, 07:59:23 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   PortStr: String;
  3. begin
  4.   PortStr := '123';
Is also a string of 3 characters.
So it's just a question of what's going wrong and what it is used for.

You don't need to shrink a string because it's dynamic length in nature.

Zvoni

  • Hero Member
  • *****
  • Posts: 3355
Re: How do i shrink the length of a string variable ?
« Reply #4 on: August 23, 2023, 10:25:55 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Var
  3.   s:String;
  4. begin
  5.   s:='ABCDEFGH';
  6.   Writeln(s);
  7.   SetLength(s,5);
  8.   Writeln(s);
  9. end.
Returns
ABCDEFGH
ABCDE
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: How do i shrink the length of a string variable ?
« Reply #5 on: August 23, 2023, 11:09:57 am »
How do i shrink the length of a string variable ?

Could you explain better what you need, possibly with an example?
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

pascalbythree

  • Sr. Member
  • ****
  • Posts: 289
Re: How do i shrink the length of a string variable ?
« Reply #6 on: August 23, 2023, 11:36:23 am »
Code: Pascal  [Select][+][-]
  1. function IntToStr(i:Longint):String;
  2. var S : String;
  3. begin
  4.  Str(i,S);
  5.  result:=S;
  6. end;

Further, does anybody know how to go from StrToInt ? The otherway around as above ?

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: How do i shrink the length of a string variable ?
« Reply #7 on: August 23, 2023, 11:40:58 am »
Code: Pascal  [Select][+][-]
  1. function IntToStr(i:Longint):String;
  2. var S : String;
  3. begin
  4.  Str(i,S);
  5.  result:=S;
  6. end;
So, what exactly is your problem now?
I'm not sure what this bit of code has to do with your question  %)

BTW. Depending on your mode you could also just do i.toString.
No need for IntToStr() function. There are a lot more helper options.

Further, does anybody know how to go from StrToInt ? The otherway around as above ?
IntToStr() or IntToStrDef() or TryStrToInt().

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: How do i shrink the length of a string variable ?
« Reply #8 on: August 23, 2023, 11:50:14 am »
Further, does anybody know how to go from StrToInt ? The otherway around as above ?

Please explain better whet you need.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How do i shrink the length of a string variable ?
« Reply #9 on: August 23, 2023, 12:13:06 pm »
Further, does anybody know how to go from StrToInt ? The otherway around as above ?
Perhaps by using "Uses SysUtils" and call IntToStr() or StrToInt() and not any selfmade stuff.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Zvoni

  • Hero Member
  • *****
  • Posts: 3355
Re: How do i shrink the length of a string variable ?
« Reply #10 on: August 23, 2023, 12:23:10 pm »
Further, does anybody know how to go from StrToInt ? The otherway around as above ?
Perhaps by using "Uses SysUtils" and call IntToStr() or StrToInt() and not any selfmade stuff.
Yepp. SysUtils to get the typeHelpers

SomeInteger.ToString;
SomeString.ToInteger;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

pascalbythree

  • Sr. Member
  • ****
  • Posts: 289
Re: How do i shrink the length of a string variable ?
« Reply #11 on: August 23, 2023, 02:50:36 pm »
On a AVR ? Or is the example above for ARM?

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: How do i shrink the length of a string variable ?
« Reply #12 on: August 23, 2023, 02:59:20 pm »
On a AVR ? Or is the example above for ARM?
The example is for Free Pascal (unit sysutils in particular as that includes the helpers) . But in case you are memory restricted then using sysutils is not the smartest idea because of its size.
« Last Edit: August 23, 2023, 03:01:05 pm by TRon »
Today is tomorrow's yesterday.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How do i shrink the length of a string variable ?
« Reply #13 on: August 23, 2023, 03:47:47 pm »
Without SysUtils in a very size saving way
Code: Pascal  [Select][+][-]
  1. function IntToStr(const i: Integer): string;
  2. begin
  3.  Str(i, Result);
  4. end;
  5.  
  6. function StrToInt(const s: string): Integer;
  7. var
  8.  code: Integer;
  9. begin
  10.  Val(s, Result, code);
  11. end;
Beware, none of this has any kind of checking ... for that you can try those:
Code: Pascal  [Select][+][-]
  1. function StrToIntDef(const s: string; const i: Integer): Integer;
  2. var
  3.  code: Integer;
  4. begin
  5.  Val(s, Result, code);
  6.   if(code <> 0) then
  7.     Result := i;
  8. end;
  9.  
  10. function TryStrToInt(const s: string; out i: Integer): Boolean;
  11. var
  12.  code, ii : integer;
  13. begin
  14.   Result := False;
  15.   ii := 0;
  16.   Val(s, ii, code);
  17.   if (code = 0) then
  18.     begin
  19.       i := ii;
  20.       Result := True;
  21.      end;
  22. end;
« Last Edit: August 23, 2023, 03:49:38 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

paweld

  • Hero Member
  • *****
  • Posts: 1596
Re: How do i shrink the length of a string variable ?
« Reply #14 on: August 23, 2023, 04:18:24 pm »
@KodeZwerg: as @rvk wrote such functions already exist, what is the purpose of duplicating them?
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018