Word Wrap

Description

This function neatly splits long texts into separate lines, without splitting in between words.

This snippet requires the addon skript-reflect to function.

Code

expression wrap %string% (using|with) [delimiter] %string% with [wrap] (width|length|size) %integer%:
    return type: string
    get:
        return expr-1 if expr-3 < 2
        return join (regex split expr-1.replaceAll("(.{1,%expr-3%})(?:\s+|$)|(.{1,%expr-3%})", "$1$2%expr-2%") at "%expr-2%$")

Usage

broadcast wrap "This is a demo string to_demonstrate the snippet." using newline with width 12
# Output:
# This is a
# demo string
# to_demonstrate ' This word is longer than the wrap amount i.e. 12 so it does not wrap it.
# the snippet.

Last updated