Page 1 of 1

Chr$(40,5) = "(((((" possible? [Solved]

Posted: Thursday 4th March 2021 6:25pm
by Godzilla
Hello,

We have a command in Gambas "Space" that will produce X number of repetitions of a space.
Print "<"; Space$(8); ">"
The result is
<        >
In certain circumstances, this ability is very helpful.

Is there a command in Gambas which would work similarly for any ASCII character, repeating it X number of times?? Or perhaps in a future release, the Chr$() command can be modified to have an optional second argument for number of repetitions? There's certain circumstances where this ability would save a lot of typing.

For example:
For Counter = 100 DownTo 2
TheString = Replace$(TheString,Chr$(40,Counter),Null,gb.Binary)
TheString = Replace$(TheString,Chr$(41,Counter),Null,gb.Binary)
Next
(((((((((((Hello!)))))
becomes
(Hello!)
Thank you.

Re: Chr$(40,5) = "(((((" possible?

Posted: Thursday 4th March 2021 6:40pm
by BruceSteers
Maybe the PadLeft PadRight commands of String from gb.util ?

http://gambaswiki.org/edit/comp/gb.util/string/padleft
wiki" wrote:
Static Function PadLeft ( String As String, Length As Integer [ , Pad As String ] ) As String


SINCE 3.15
Returns a new string that right-aligns the characters in the original string by padding them on the left with a specified string pattern, for a specified total length.

String is the string to pad.

Length is the final length in UTF-8 characters.

Pad is the string pattern used for padding. By default, a space is used.

Examples

Print String.PadLeft("Gambas", 10, ".")
....Gambas

Print String.PadLeft("Gambas", 16, "×->")
×->×->×->×Gambas

BruceS

Re: Chr$(40,5) = "(((((" possible?

Posted: Thursday 4th March 2021 6:41pm
by cogier
Try this: -
Print String(5, Chr(40))
The output is: -

(((((

Re: Chr$(40,5) = "(((((" possible?

Posted: Friday 5th March 2021 6:48am
by Godzilla
Bruce and cogier, both very workable and useful solutions. Thank you for your replies!

Re: Chr$(40,5) = "(((((" possible? [Solved]

Posted: Friday 5th March 2021 5:25pm
by BruceSteers
Hehe you are welcome.

My answer i thought was just how I'd do it with my knowledge.

Cogier seemed to hit the nail right on the head with the exact solution though
(i didn't know you could even do that with String() :lol: )

PadLeft PadRight have other uses though and are also good commands to know of :)


knowledge is power :)