Skip to main content

Strings

The Strings category covers functions for building, reshaping, searching, and measuring text. You can call each one in prefix form fn(s, …) or in postfix/UFCS form s fn(…).

Some text operations you might expect here live in Core instead, because they generalize beyond strings: upper, lower, trim, replace (literal), contains, startsWith, endsWith, indexOf, splitBy, and joinBy.

Casing and Inflection

These apply natural-language transforms.

FunctionSignatureDescriptionExample
camelize(:string)Splits on non-alphanumerics; lowercases the first word, TitleCases the rest, joins them.camelize("hello_world")"helloWorld"
capitalize(:string)First character upper, the rest lower.capitalize("hELLO")"Hello"
dasherize(:string)Normalizes spaces/underscores/dashes and returns a kebab-cased string.dasherize("Hello World")"hello-world"
underscore(:string)Snake-cases the input (dashes and spaces become underscores).underscore("helloWorld")"hello_world"
ordinalize(:number)The ordinal string for a number.ordinalize(3)"3rd"
pluralize(:string, :number?)Pluralizes a word; with a count, pluralizes only when the count isn't 1.pluralize("person")"people"; pluralize("apple", 1)"apple"
singularize(:string)Singularizes a word.singularize("people")"person"

Padding and Wrapping

FunctionSignatureDescriptionExample
leftPad(:string, :number, :string?)Pads on the left to a target length (pad string defaults to a space).leftPad("7", 3, "0")"007"
rightPad(:string, :number, :string?)Pads on the right to a target length.rightPad("7", 3, ".")"7.."
repeat(:string, :number)Repeats the string N times.repeat("ab", 3)"ababab"
wrapWith(:string, :string)Wraps the value with the given string on both sides, unconditionally.wrapWith("x", "*")"*x*"
wrapIfMissing(:string, :string)Adds the wrapper to each end only where it isn't already present.wrapIfMissing("*x", "*")"*x*"
unwrap(:string, :string)Removes the wrapper from both ends when present on both.unwrap("*x*", "*")"x"
appendIfMissing(:string, :string, :any?)Appends the suffix unless it's already there (3rd arg = ignore case).appendIfMissing("file", ".txt")"file.txt"
prependIfMissing(:string, :string, :any?)Prepends the prefix unless it's already there.prependIfMissing("path", "/")"/path"

Search and Tests

FunctionSignatureDescriptionExample
countMatches(:string, :string)Counts regex matches of the pattern in the string.countMatches("a-b-c", "-")2
isAlpha(:string)True if non-empty and all letters.isAlpha("abc")true
isAlphanumeric(:string)True if non-empty and all letters/digits.isAlphanumeric("ab12")true
isLowerCase(:string)True if non-empty and all lowercase.isLowerCase("abc")true
isUpperCase(:string)True if non-empty and all uppercase.isUpperCase("ABC")true
isWhitespace(:string)True if empty or all whitespace.isWhitespace(" ")true
isNumeric(:any)True only if the value is a Number (not a numeric string).isNumeric(42)true; isNumeric("42")false

Substrings and Truncation

FunctionSignatureDescriptionExample
substring(:string, :number, :number?)Slice from a start index; with a length, that many chars, else to the end.substring("hello", 1, 3)"ell"
substringBefore(:string, :string)Text before the first occurrence of a separator.substringBefore("a=b=c", "=")"a"
substringAfter(:string, :string)Text after the first occurrence.substringAfter("a=b=c", "=")"b=c"
substringBeforeLast(:string, :string)Text before the last occurrence.substringBeforeLast("a=b=c", "=")"a=b"
substringAfterLast(:string, :string)Text after the last occurrence.substringAfterLast("a=b=c", "=")"c"
substringEvery(:string, :number)Chunks the string into fixed-size pieces (an array).substringEvery("abcdef", 2)["ab","cd","ef"]
first(:string, :number?)The first N characters (default 1).first("hello", 2)"he"
last(:string, :number?)The last N characters (default 1).last("hello", 2)"lo"
withMaxSize(:string, :number)Caps length to N, keeping the last N characters if longer.withMaxSize("hello", 3)"llo"
remove(:string, :string)Removes every regex match of the pattern.remove("a1b2", "[0-9]")"ab"
replaceAll(:string, :string, :string)Literal (non-regex) replacement of all occurrences.replaceAll("a.b.c", ".", "-")"a-b-c"

Lines, Words, and Runs

FunctionSignatureDescriptionExample
lines(:string)Splits on \r\n, \r, or \n into an array of lines.lines("a\nb")["a","b"]
words(:string)Extracts alphanumeric (plus apostrophe) words.words("it's a test")["it's","a","test"]
collapse(:string)Groups runs of identical consecutive characters.collapse("aabbb")["aa","bbb"]

Character-Level and Code Points

FunctionSignatureDescriptionExample
charCode(:string)Code point of the first character.charCode("A")65
charCodeAt(:string, :number)Code point at a given index.charCodeAt("ABC", 1)66
fromCharCode(:number)Character for a Unicode code point.fromCharCode(65)"A"
mapString(:string, :lambda)Maps each character (char, index) to a string and concatenates.mapString("abc", (c) -> upper(c))"ABC"
everyCharacter(:string, :lambda)True if every character satisfies the predicate.everyCharacter("abc", $ isAlpha())true
someCharacter(:string, :lambda)True if any character satisfies the predicate.someCharacter("a1c", $ isNumeric())false
countCharactersBy(:string, :lambda)Counts characters satisfying the predicate.countCharactersBy("a1b2", (c) -> c isNumeric())0
substringBy(:string, :lambda)Splits into segments, treating chars where the lambda is truthy as delimiters.substringBy("a,b;c", (c) -> c == "," or c == ";")["a","b","c"]

Distance Metrics

FunctionSignatureDescriptionExample
hammingDistance(:string, :string)Count of differing positions; requires equal-length inputs (raises otherwise).hammingDistance("karolin", "kathrin")3
levenshteinDistance(:string, :string)Minimum single-character edits to turn one string into the other.levenshteinDistance("kitten", "sitting")3

Reversal

FunctionSignatureDescriptionExample
reverse(:any)Reverses a String, Array, or Object (entry order).reverse("abc")"cba"