Recent

Author Topic: Is there a lazarus function f('a',3) = 'aaa' ?  (Read 2303 times)

gabriel

  • Newbie
  • Posts: 4
Is there a lazarus function f('a',3) = 'aaa' ?
« on: July 14, 2021, 10:31:09 am »
I have been looking in documentation to find a simple function, that takes a string s and number n as arguments. The result should be the string s repeated n times.

f('a',3) should give the result 'aaa'.

Of cause I can easily write a function myself. I don't need help for that. But I am sure Lazarus/Freepascal has  a ready made function, but I just can't find it.

 If you have a clue please let me know.

gabriel

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Is there a lazarus function f('a',3) = 'aaa' ?
« Reply #1 on: July 14, 2021, 10:37:26 am »
https://www.freepascal.org/docs-html/rtl/strutils/addchar.html
https://www.freepascal.org/docs-html/rtl/strutils/addcharr.html

Code: Pascal  [Select][+][-]
  1. result:=addchar('a',3,'');

for spaces specifically

Code: Pascal  [Select][+][-]
  1. writestr(result,' ':3);

strutils in general is a mixed string utils units, always worth looking at if you are searching for something

Dupestring is the same but for whole strings, which can also happen to be one char long (and works fine with UTF8!)

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

Dseligo below mentioned stringofchar (which I missed): https://www.freepascal.org/docs-html/rtl/system/stringofchar.html
« Last Edit: July 14, 2021, 10:39:50 am by marcov »

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Is there a lazarus function f('a',3) = 'aaa' ?
« Reply #2 on: July 14, 2021, 10:38:30 am »
For string you can use DupeString in StrUtils, for repeating characters you can use StringOfChar.

gabriel

  • Newbie
  • Posts: 4
Re: Is there a lazarus function f('a',3) = 'aaa' ?
« Reply #3 on: July 14, 2021, 12:01:40 pm »
Thank you very much.

Dupstring was what I was looking for.

Gabriel

 

TinyPortal © 2005-2018