How to add ellipsis if string length exceeded 20 characters?

I need to shorten strings stored in an array variable if the character length exceeds 20 characters and add trailing ellipsis (…) if the string was shortened. I know theres the substring method to easily shorten the string but how can I add the ellipsis IF the string was shortened? The docs are a bit limited in terms of if statements. Any insight is hugely appreciated!

Rules:

  • If a string is longer than 20 characters, remove the last 3 characters and replace with ellipsis.
  • If a string length is between 0 and 20 characters, do nothing.

Example Input:

["short string", "medium length string", "long string that contains more than 20 characters"]

Desired Output:

["short string", "medium length string", "long string that con..."]

Bundle Output Example:

[
    {
        "strings": [
            "short string",
            "medium length string",
            "long string that contains more than 20 characters",
        ]
    }
]

This rule is ambiguous or possibly incorrect.

  1. This string is 30 characters long

    123456789012345678901234567890

  2. Remove last 3 characters

    123456789012345678901234567

  3. Add three ellipsis

    123456789012345678901234567...

Therefore,

long string that contains more than 20 characters

can never be

long string that con...

which is 23 characters long

So did you mean this instead?

By the way, three periods ... is different than a single ellipsis character

It is important to provide accurate instructions, otherwise you can potentially waste others’ time.

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

Welcome to the Make community!

Yes, that is possible. You’ll need about three modules (assuming you want the result to be an array). This will take fewer operations compared to if you placed the set variable within the Iterator loop.:

You can check the length of the string by using the built-in function length

Screenshot_20240619_094648

If yes, truncate the string using the built-in function substring

If no, return the value.

Output

Screenshot_20240619_093817

Give it a go and let us know if you have any issues!

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

This is perfect, thanks so much for the solution. You are correct in that I miscounted in my example. I appreciate your help