Tinderbox Forum

Function to build a string by repeating a substring

I’ve looked in the forum and on atbref, but can’t find anything — but I may not have used the right search terms. Is there a function which will build a string from another string repeated X times?

E.g. something which would work like this:

$MyString = repeat("#", X)

… which would result in $MyString=="####" when X==4

The nearest I can get to is to use substr: e.g.

$MyString = substr("#######",0,4)

which works, but it feels a bit clunky, and it means I have to guess the maximum number of characters in the ‘dummy’ string.

Context

I want to use it in Export code, and the # probably gives the game away that I want to create the right level of markdown headers for descendants of the selected notes. I want to make sure that whichever level I choose to export, the hierarchy starts with #, children are ## and so on.

The following works, but I wondered if there was a simpler way to build the header.

Result:

Thanks!

(Obviously, if there’s an easier approach to build the hierarchy, it would be good to know that too…)

I think you are looking for the ^indent()^ code using a # rather than the default tab and its action code version indent().

1 Like

Thanks, Mark — that works fine and is simpler than the substr method. I appreciate your help, as always!

1 Like

Note, too, that you can multiply strings!

 $MyString = 3*"No ";    ➛ "No No No"
1 Like

Hah – I thought there may be a dot operator to achieve it, but it’s even simpler than that…

Many thanks!

For your specific use, ^indent() probably makes your intent clearer. But it’s good to know the general case as well.

Indeed: it was the general answer I was after in posting—I had a w0rking solution with substr but wanted a better way. And now I have a general and a specific solution — thanks!