[Extension] String formatting - ...format strings

Hello dear community, it’s ya boy with another extension. Now, I know I said I’m taking a break for a while, but I realised I have been very lazy before I did and since I caught some free time recently I started clearing my to do list, which is actually mostly extensions and games.

So, I present to you: The string formatting extension!

Blocks (actually, just one)

The string formatting extension adds only one block to your namespace, as a part of the already existing Text drawer.

function format(text: string, params: string[]): string

image

As you can see, it takes in two arguments. One, the string to be formatted, and the parameters.

How to use

The extension works on a very simple principle. Use open curly brackets (“{}”) to create a placeholder in your string. Once you use the format method, the placeholders will be filled in from left to right with the provided parameters. Here’s an example:

In this example, “8” will be mapped to the first field, “14” to the second, and “44” to the third. Those placeholders will be replaced by the values and the output will be “8:14:44”.

A few things to note:

  • Even if you’re passing only one value, it still has to be in an array
  • The parameters must be strings. I may add support for inputting numbers natively soon
  • Providing too little parameters (eg. creating 3 placeholders and providing 2 parameters) will result in an exception (this is intentional, arcade will display the error message to you)
  • If you provide too many parameters, the excess values will be ignored
  • Incorrectly written or closed fields will be ignored by the parser (must be “{}”, no whitespace)

Escape characters

In case you wish to write curly braces in your text in the same way as the placeholder syntax (for some reason), I’ve got you covered. In order to do so, simply put a backslash* (“\”) before the first curly bracket. That looks something like this:

(“Bob” replaces the second placeholder because the first one is escaped)
This outputs “:{} ← His name is Bob”

Now, this creates a problem because if you wanted to place a single backslash, the formatter would think you’re trying to escape a character. To solve this, simply put another backslash before your backslash to render one backslash**

This outputs “C:\Users\Sarge”, because the backslashes are both escaped, so the placeholders aren’t.


* Due to JavaScript strings also escaping backslashes, you need to type “\\{” if you’re using JS
** Same reason as above, to escape a backslash use “\\\\” (I know, I know)


You can find the extension here

Thanks for reading. Now, I’m out of here, I have more extensions to work on

9 Likes

Seems like a great shortcut, and useful for RPG style games where a bunch of text has to be replaced! :sparkling_heart:

2 Likes

Cool.

3 Likes

Woah this makes custom text waaay easier! For floor 13 I had to make some seriously convoluted code for the floor text lol

3 Likes

Imporant announcement

I changed my GitHub username, so please remove this extension in any project you may have it imported, and instead import the new link under my new username (sargedev).

This is not necessary, as the extension should still work, but it would be the safest option. By importing the new link you will also get access to any new updates.

WARNING: Do not import the new link before removing the old one, as this could cause conflicts

4 Likes