String Manipulation
How to "edit" a string
Preface
Before learning the string manipulation
functions, let's see the following codes first.
What are the differences between the two codes above? The outputs are the same, but the way to make it is different.
joinStr
function allows you to combine multiple strings into one, which is needed in embeds, sendMessage
function, editMessage
function, etc. \n
means to change the line. Be warned, you can't just press enter
to change the line whenever you use functions to output a message.
String Manipulation
functions are useful when you want to "edit" your string. You can either change the case of letters, change the contents a little, or use reFind
function to find things you need. After manipulating the string, it will be better than the previous one and have more ways to use.
Function
Function
Description
joinStr "separator" "str1" (arg1)(arg2) "str2" ...
Joins several strings into one, separated by the first argument"separator"
. Example:{{joinStr "" "1" "2" "3"}}
returns 123
. Also if functions have string or easily convertible return, they can be used inside joinStr
e.g. {{joinStr "" "Let's calculate " (add (mult 13 3) 1 2) ", was returned at " (currentTime.Format "15:04") "."}}
lower "string"
Converts the string to lowercase.
upper "string"
Converts the string to uppercase.
slice "string"|slice integer (integer2)
The function's first argument must be of type string or slice.
urlescape "string"
Escapes the string so it can be safely placed inside a URL path segment - e.g. "Hello, YAGPDB!" becomes "Hello%2C%20YAGPDB%21"
split "string" "sepr"
title "string"
Returns string with the first letter of each word capitalized.
reFind "regex" "string"
Compares "string" to regex pattern and returns first match. {{reFind "AG" "YAGPDB is cool!"}}
returns AG
(regex pattern is case sensitive).
reFindAll "regex" "string"
reFindAllSubmatches "regex" "string"
Returns whole-pattern matches and also the sub-matches within those matches as slices inside a slice. {{reFindAllSubmatches "(?i)y([a-z]+)g" "youngish YAGPDB"}}
returns [[young oun] [YAG A]]
(regex pattern here is case insensitive).
reReplace "regex" "string1" "string2"
Replaces "string1" contents with "string2" at regex match point. {{reReplace "I am" "I am cool!" "YAGPDB is"}}
returns YAGPDB is cool!
(regex pattern here is case sensitive).
print, printf, println
Example:
{{$args:= (joinStr " " (slice .CmdArgs 1))}}
Saves all the arguments except the first one to a variable$args
. Example: if you the whole trigger is "-hi" and you type in "-hi hello how are you", it will saves "how are you" to the$args
.To demonstrate usage of
split
function.{{$x := "Hello, World, YAGPDB, here!"}} {{range $k, $v := (split $x ", ")}}Word {{$k}}: __{{$v}}__ {{end}}
This will output "Word 0: Hello Word 1: World Word 2: YAGPDB Word 3: here!" (range action here).
To demonstrate usage of
reFindAll
. >Before regex: {{$msg := "1 YAGPDB and over 100000 servers conquered."}} {{$re2 := reFindAll "[0-9]+" $msg}} {{$msg}} After regex matches: {{joinStr " " "Only" (index $re2 0) "YAGPDB and already" (index $re2 1) "servers captured."}}
This will output like the following picture:
Last updated
Was this helpful?