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

Post your Gambas programming questions here.
Post Reply
User avatar
Godzilla
Posts: 63
Joined: Wednesday 26th September 2018 11:20am

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

Post 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.
Last edited by Godzilla on Friday 5th March 2021 6:48am, edited 1 time in total.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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
Last edited by BruceSteers on Thursday 4th March 2021 6:43pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post by cogier »

Try this: -
Print String(5, Chr(40))
The output is: -

(((((
User avatar
Godzilla
Posts: 63
Joined: Wednesday 26th September 2018 11:20am

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

Post by Godzilla »

Bruce and cogier, both very workable and useful solutions. Thank you for your replies!
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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 :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply