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

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

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

Post by Godzilla » Thursday 04th March 2021 6:25pm

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 05th March 2021 6:48am, edited 1 time in total.

User avatar
BruceSteers
Posts: 441
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Thursday 04th March 2021 6:40pm

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 04th March 2021 6:43pm, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 672
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post by cogier » Thursday 04th March 2021 6:41pm

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

(((((

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

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

Post by Godzilla » Friday 05th March 2021 6:48am

Bruce and cogier, both very workable and useful solutions. Thank you for your replies!

User avatar
BruceSteers
Posts: 441
Joined: Thursday 23rd July 2020 5:20pm

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

Post by BruceSteers » Friday 05th March 2021 5:25pm

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 :)
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply