String Manipulation
How to "edit" a string
Preface
Before learning the string manipulation
functions, let's see the following codes first.
Hi, {{.User.Mention}}. {{.Guild.Name}} is glad to see you.
The icon hash ID of the server is {{.Guild.Icon}}.
Where are you? You are in the server which is in {{.Guild.Region}}.
Thank you for joining and let's see your nice avatar: {{.User.AvatarURL "1024"}}
{{(joinStr " " "Hi," .User.Mention "." .Guild.Name "is glad to see you.\nThe icon hash ID of the server is" .Guild.Icon ".\nWhere are you? You are in the server which is in" .Guild.Region ".\nThank you for joining and let's see your nice avatar:" (.User.AvatarURL "1024"))}}
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.
Outputs the "string" after cutting/slicing off integer (numeric) value of symbols (actually starting the string's index from integer through integer2) - e.g. {{slice "Fox runs" 2}}
outputs x runs
. When using also integer2 - e.g. {{slice "Fox runs" 1 7}}
, it outputs ox run
. For slicing whole arguments, let's say words, see example in section's Snippets.
This slice
function is not the same as basic dynamically-sized slice data type discussed in this reference doc. Also it's custom, not having 3-indices as the default one from text/template package.
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"
Splits given "string"
to substrings separated by "sepr"
arg and returns new slice of the substrings between given separator e.g. {{split "YAG, is cool!" ","}}
returns [YAG is cool!]
slice where YAG
is at index
position 0 and is cool!
at index
position 1. Example also in section's Snippets.
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"
Adds all regex matches from the "string" to a slice. Example in section's Snippets.
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
These are GO template package's predefined functions and are aliases for fmt.Sprint, fmt.Sprintf and fmt.Sprintln. Formatting is also discussed here.
printf
is usable for example to determine the type of the value > {{printf "%T" currentTime}}
outputs currentTime
functions output value type of time.Time
. In many cases, printf
is a great alternative to joinStr
for concatenate strings.
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?