| 1 |
# String Functions |
| 2 |
|
| 3 |
Sprig has a number of string manipulation functions. |
| 4 |
|
| 5 |
## trim |
| 6 |
|
| 7 |
The `trim` function removes space from either side of a string: |
| 8 |
|
| 9 |
``` |
| 10 |
trim " hello " |
| 11 |
``` |
| 12 |
|
| 13 |
The above produces `hello` |
| 14 |
|
| 15 |
## trimAll |
| 16 |
|
| 17 |
Remove given characters from the front or back of a string: |
| 18 |
|
| 19 |
``` |
| 20 |
trimAll "$" "$5.00" |
| 21 |
``` |
| 22 |
|
| 23 |
The above returns `5.00` (as a string). |
| 24 |
|
| 25 |
## trimSuffix |
| 26 |
|
| 27 |
Trim just the suffix from a string: |
| 28 |
|
| 29 |
``` |
| 30 |
trimSuffix "-" "hello-" |
| 31 |
``` |
| 32 |
|
| 33 |
The above returns `hello` |
| 34 |
|
| 35 |
## trimPrefix |
| 36 |
|
| 37 |
Trim just the prefix from a string: |
| 38 |
|
| 39 |
``` |
| 40 |
trimPrefix "-" "-hello" |
| 41 |
``` |
| 42 |
|
| 43 |
The above returns `hello` |
| 44 |
|
| 45 |
## upper |
| 46 |
|
| 47 |
Convert the entire string to uppercase: |
| 48 |
|
| 49 |
``` |
| 50 |
upper "hello" |
| 51 |
``` |
| 52 |
|
| 53 |
The above returns `HELLO` |
| 54 |
|
| 55 |
## lower |
| 56 |
|
| 57 |
Convert the entire string to lowercase: |
| 58 |
|
| 59 |
``` |
| 60 |
lower "HELLO" |
| 61 |
``` |
| 62 |
|
| 63 |
The above returns `hello` |
| 64 |
|
| 65 |
## title |
| 66 |
|
| 67 |
Convert to title case: |
| 68 |
|
| 69 |
``` |
| 70 |
title "hello world" |
| 71 |
``` |
| 72 |
|
| 73 |
The above returns `Hello World` |
| 74 |
|
| 75 |
## untitle |
| 76 |
|
| 77 |
Remove title casing. `untitle "Hello World"` produces `hello world`. |
| 78 |
|
| 79 |
## repeat |
| 80 |
|
| 81 |
Repeat a string multiple times: |
| 82 |
|
| 83 |
``` |
| 84 |
repeat 3 "hello" |
| 85 |
``` |
| 86 |
|
| 87 |
The above returns `hellohellohello` |
| 88 |
|
| 89 |
## substr |
| 90 |
|
| 91 |
Get a substring from a string. It takes three parameters: |
| 92 |
|
| 93 |
- start (int) |
| 94 |
- end (int) |
| 95 |
- string (string) |
| 96 |
|
| 97 |
``` |
| 98 |
substr 0 5 "hello world" |
| 99 |
``` |
| 100 |
|
| 101 |
The above returns `hello` |
| 102 |
|
| 103 |
## nospace |
| 104 |
|
| 105 |
Remove all whitespace from a string. |
| 106 |
|
| 107 |
``` |
| 108 |
nospace "hello w o r l d" |
| 109 |
``` |
| 110 |
|
| 111 |
The above returns `helloworld` |
| 112 |
|
| 113 |
## trunc |
| 114 |
|
| 115 |
Truncate a string (and add no suffix) |
| 116 |
|
| 117 |
``` |
| 118 |
trunc 5 "hello world" |
| 119 |
``` |
| 120 |
|
| 121 |
The above produces `hello`. |
| 122 |
|
| 123 |
``` |
| 124 |
trunc -5 "hello world" |
| 125 |
``` |
| 126 |
|
| 127 |
The above produces `world`. |
| 128 |
|
| 129 |
## abbrev |
| 130 |
|
| 131 |
Truncate a string with ellipses (`...`) |
| 132 |
|
| 133 |
Parameters: |
| 134 |
|
| 135 |
- max length |
| 136 |
- the string |
| 137 |
|
| 138 |
``` |
| 139 |
abbrev 5 "hello world" |
| 140 |
``` |
| 141 |
|
| 142 |
The above returns `he...`, since it counts the width of the ellipses against the |
| 143 |
maximum length. |
| 144 |
|
| 145 |
## abbrevboth |
| 146 |
|
| 147 |
Abbreviate both sides: |
| 148 |
|
| 149 |
``` |
| 150 |
abbrevboth 5 10 "1234 5678 9123" |
| 151 |
``` |
| 152 |
|
| 153 |
the above produces `...5678...` |
| 154 |
|
| 155 |
It takes: |
| 156 |
|
| 157 |
- left offset |
| 158 |
- max length |
| 159 |
- the string |
| 160 |
|
| 161 |
## initials |
| 162 |
|
| 163 |
Given multiple words, take the first letter of each word and combine. |
| 164 |
|
| 165 |
``` |
| 166 |
initials "First Try" |
| 167 |
``` |
| 168 |
|
| 169 |
The above returns `FT` |
| 170 |
|
| 171 |
## randAlphaNum, randAlpha, randNumeric, and randAscii |
| 172 |
|
| 173 |
These four functions generate cryptographically secure (uses ```crypto/rand```) |
| 174 |
random strings, but with different base character sets: |
| 175 |
|
| 176 |
- `randAlphaNum` uses `0-9a-zA-Z` |
| 177 |
- `randAlpha` uses `a-zA-Z` |
| 178 |
- `randNumeric` uses `0-9` |
| 179 |
- `randAscii` uses all printable ASCII characters |
| 180 |
|
| 181 |
Each of them takes one parameter: the integer length of the string. |
| 182 |
|
| 183 |
``` |
| 184 |
randNumeric 3 |
| 185 |
``` |
| 186 |
|
| 187 |
The above will produce a random string with three digits. |
| 188 |
|
| 189 |
## wrap |
| 190 |
|
| 191 |
Wrap text at a given column count: |
| 192 |
|
| 193 |
``` |
| 194 |
wrap 80 $someText |
| 195 |
``` |
| 196 |
|
| 197 |
The above will wrap the string in `$someText` at 80 columns. |
| 198 |
|
| 199 |
## wrapWith |
| 200 |
|
| 201 |
`wrapWith` works as `wrap`, but lets you specify the string to wrap with. |
| 202 |
(`wrap` uses `\n`) |
| 203 |
|
| 204 |
``` |
| 205 |
wrapWith 5 "\t" "Hello World" |
| 206 |
``` |
| 207 |
|
| 208 |
The above produces `hello world` (where the whitespace is an ASCII tab |
| 209 |
character) |
| 210 |
|
| 211 |
## contains |
| 212 |
|
| 213 |
Test to see if one string is contained inside of another: |
| 214 |
|
| 215 |
``` |
| 216 |
contains "cat" "catch" |
| 217 |
``` |
| 218 |
|
| 219 |
The above returns `true` because `catch` contains `cat`. |
| 220 |
|
| 221 |
## hasPrefix and hasSuffix |
| 222 |
|
| 223 |
The `hasPrefix` and `hasSuffix` functions test whether a string has a given |
| 224 |
prefix or suffix: |
| 225 |
|
| 226 |
``` |
| 227 |
hasPrefix "cat" "catch" |
| 228 |
``` |
| 229 |
|
| 230 |
The above returns `true` because `catch` has the prefix `cat`. |
| 231 |
|
| 232 |
## quote and squote |
| 233 |
|
| 234 |
These functions wrap a string in double quotes (`quote`) or single quotes |
| 235 |
(`squote`). |
| 236 |
|
| 237 |
## cat |
| 238 |
|
| 239 |
The `cat` function concatenates multiple strings together into one, separating |
| 240 |
them with spaces: |
| 241 |
|
| 242 |
``` |
| 243 |
cat "hello" "beautiful" "world" |
| 244 |
``` |
| 245 |
|
| 246 |
The above produces `hello beautiful world` |
| 247 |
|
| 248 |
## indent |
| 249 |
|
| 250 |
The `indent` function indents every line in a given string to the specified |
| 251 |
indent width. This is useful when aligning multi-line strings: |
| 252 |
|
| 253 |
``` |
| 254 |
indent 4 $lots_of_text |
| 255 |
``` |
| 256 |
|
| 257 |
The above will indent every line of text by 4 space characters. |
| 258 |
|
| 259 |
## nindent |
| 260 |
|
| 261 |
The `nindent` function is the same as the indent function, but prepends a new |
| 262 |
line to the beginning of the string. |
| 263 |
|
| 264 |
``` |
| 265 |
nindent 4 $lots_of_text |
| 266 |
``` |
| 267 |
|
| 268 |
The above will indent every line of text by 4 space characters and add a new |
| 269 |
line to the beginning. |
| 270 |
|
| 271 |
## replace |
| 272 |
|
| 273 |
Perform simple string replacement. |
| 274 |
|
| 275 |
It takes three arguments: |
| 276 |
|
| 277 |
- string to replace |
| 278 |
- string to replace with |
| 279 |
- source string |
| 280 |
|
| 281 |
``` |
| 282 |
"I Am Henry VIII" | replace " " "-" |
| 283 |
``` |
| 284 |
|
| 285 |
The above will produce `I-Am-Henry-VIII` |
| 286 |
|
| 287 |
## plural |
| 288 |
|
| 289 |
Pluralize a string. |
| 290 |
|
| 291 |
``` |
| 292 |
len $fish | plural "one anchovy" "many anchovies" |
| 293 |
``` |
| 294 |
|
| 295 |
In the above, if the length of the string is 1, the first argument will be |
| 296 |
printed (`one anchovy`). Otherwise, the second argument will be printed |
| 297 |
(`many anchovies`). |
| 298 |
|
| 299 |
The arguments are: |
| 300 |
|
| 301 |
- singular string |
| 302 |
- plural string |
| 303 |
- length integer |
| 304 |
|
| 305 |
NOTE: Sprig does not currently support languages with more complex pluralization |
| 306 |
rules. And `0` is considered a plural because the English language treats it |
| 307 |
as such (`zero anchovies`). The Sprig developers are working on a solution for |
| 308 |
better internationalization. |
| 309 |
|
| 310 |
## snakecase |
| 311 |
|
| 312 |
Convert string from camelCase to snake_case. |
| 313 |
|
| 314 |
``` |
| 315 |
snakecase "FirstName" |
| 316 |
``` |
| 317 |
|
| 318 |
This above will produce `first_name`. |
| 319 |
|
| 320 |
## camelcase |
| 321 |
|
| 322 |
Convert string from snake_case to CamelCase |
| 323 |
|
| 324 |
``` |
| 325 |
camelcase "http_server" |
| 326 |
``` |
| 327 |
|
| 328 |
This above will produce `HttpServer`. |
| 329 |
|
| 330 |
## kebabcase |
| 331 |
|
| 332 |
Convert string from camelCase to kebab-case. |
| 333 |
|
| 334 |
``` |
| 335 |
kebabcase "FirstName" |
| 336 |
``` |
| 337 |
|
| 338 |
This above will produce `first-name`. |
| 339 |
|
| 340 |
## swapcase |
| 341 |
|
| 342 |
Swap the case of a string using a word based algorithm. |
| 343 |
|
| 344 |
Conversion algorithm: |
| 345 |
|
| 346 |
- Upper case character converts to Lower case |
| 347 |
- Title case character converts to Lower case |
| 348 |
- Lower case character after Whitespace or at start converts to Title case |
| 349 |
- Other Lower case character converts to Upper case |
| 350 |
- Whitespace is defined by unicode.IsSpace(char) |
| 351 |
|
| 352 |
``` |
| 353 |
swapcase "This Is A.Test" |
| 354 |
``` |
| 355 |
|
| 356 |
This above will produce `tHIS iS a.tEST`. |
| 357 |
|
| 358 |
## shuffle |
| 359 |
|
| 360 |
Shuffle a string. |
| 361 |
|
| 362 |
``` |
| 363 |
shuffle "hello" |
| 364 |
``` |
| 365 |
|
| 366 |
The above will randomize the letters in `hello`, perhaps producing `oelhl`. |
| 367 |
|
| 368 |
## regexMatch, mustRegexMatch |
| 369 |
|
| 370 |
Returns true if the input string contains any match of the regular expression. |
| 371 |
|
| 372 |
``` |
| 373 |
regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" "[email protected]" |
| 374 |
``` |
| 375 |
|
| 376 |
The above produces `true` |
| 377 |
|
| 378 |
`regexMatch` panics if there is a problem and `mustRegexMatch` returns an error to the |
| 379 |
template engine if there is a problem. |
| 380 |
|
| 381 |
## regexFindAll, mustRegexFindAll |
| 382 |
|
| 383 |
Returns a slice of all matches of the regular expression in the input string. |
| 384 |
The last parameter n determines the number of substrings to return, where -1 means return all matches |
| 385 |
|
| 386 |
``` |
| 387 |
regexFindAll "[2,4,6,8]" "123456789" -1 |
| 388 |
``` |
| 389 |
|
| 390 |
The above produces `[2 4 6 8]` |
| 391 |
|
| 392 |
`regexFindAll` panics if there is a problem and `mustRegexFindAll` returns an error to the |
| 393 |
template engine if there is a problem. |
| 394 |
|
| 395 |
## regexFind, mustRegexFind |
| 396 |
|
| 397 |
Return the first (left most) match of the regular expression in the input string |
| 398 |
|
| 399 |
``` |
| 400 |
regexFind "[a-zA-Z][1-9]" "abcd1234" |
| 401 |
``` |
| 402 |
|
| 403 |
The above produces `d1` |
| 404 |
|
| 405 |
`regexFind` panics if there is a problem and `mustRegexFind` returns an error to the |
| 406 |
template engine if there is a problem. |
| 407 |
|
| 408 |
## regexReplaceAll, mustRegexReplaceAll |
| 409 |
|
| 410 |
Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. |
| 411 |
Inside string replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch |
| 412 |
|
| 413 |
``` |
| 414 |
regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W" |
| 415 |
``` |
| 416 |
|
| 417 |
The above produces `-W-xxW-` |
| 418 |
|
| 419 |
`regexReplaceAll` panics if there is a problem and `mustRegexReplaceAll` returns an error to the |
| 420 |
template engine if there is a problem. |
| 421 |
|
| 422 |
## regexReplaceAllLiteral, mustRegexReplaceAllLiteral |
| 423 |
|
| 424 |
Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement |
| 425 |
The replacement string is substituted directly, without using Expand |
| 426 |
|
| 427 |
``` |
| 428 |
regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}" |
| 429 |
``` |
| 430 |
|
| 431 |
The above produces `-${1}-${1}-` |
| 432 |
|
| 433 |
`regexReplaceAllLiteral` panics if there is a problem and `mustRegexReplaceAllLiteral` returns an error to the |
| 434 |
template engine if there is a problem. |
| 435 |
|
| 436 |
## regexSplit, mustRegexSplit |
| 437 |
|
| 438 |
Slices the input string into substrings separated by the expression and returns a slice of the substrings between those expression matches. The last parameter `n` determines the number of substrings to return, where `-1` means return all matches |
| 439 |
|
| 440 |
``` |
| 441 |
regexSplit "z+" "pizza" -1 |
| 442 |
``` |
| 443 |
|
| 444 |
The above produces `[pi a]` |
| 445 |
|
| 446 |
`regexSplit` panics if there is a problem and `mustRegexSplit` returns an error to the |
| 447 |
template engine if there is a problem. |
| 448 |
|
| 449 |
## regexQuoteMeta |
| 450 |
|
| 451 |
Returns a string that escapes all regular expression metacharacters inside the argument text; |
| 452 |
the returned string is a regular expression matching the literal text. |
| 453 |
|
| 454 |
``` |
| 455 |
regexQuoteMeta "1.2.3" |
| 456 |
``` |
| 457 |
|
| 458 |
The above produces `1\.2\.3` |
| 459 |
|
| 460 |
## See Also... |
| 461 |
|
| 462 |
The [Conversion Functions](conversion.html) contain functions for converting |
| 463 |
strings. The [String Slice Functions](string_slice.html) contains functions |
| 464 |
for working with an array of strings. |