A utility library for working with strings in AutoHotkey 2. Inspired by underscore.string.
The recommended approach for installation is to use ahkpm.
ahkpm install gh:joshuacc/strung.ahk
# Run the following to get the #Include statement to put into your files
ahkpm include gh:joshuacc/strung.ahk
Strung.ahk has an extensive set of utility methods for manipulating strings. You can use these utilities in two different ways.
Firstly, you can use the static methods. These each require passing in the string you are operating on as the first argument. For example:
; This path may be different on your system
#Include %A_ScriptDir%\ahkpm-modules\github.com\joshuacc\strung.ahk\strung.ahk
Strung.leftPad("abc", 3)
; => " abc"
Secondly, you can use the instance methods avilable on the chaining API, and use
.value()
to get the final result.
; This path may be different on your system
#Include %A_ScriptDir%\ahkpm-modules\github.com\joshuacc\strung.ahk\strung.ahk
Strung("abc").leftPad(3).value()
; => " abc"
Why use the chaining API? When performing multi-step operations on strings, it allows you to avoid keeping track of intermediate results, and also lets you list the order of operations in a straightforward way.
For example:
Strung("abcdefg").truncate(3).toUpperCase().surround("~").value()
; => "~ABC~"
Rules for the chaining API:
- When using the chaining API, you must omit the
str
parameter found in the static API method - Use
.value()
to get the final result after a series of string operations. - Any methods that return a non-string value (such as
.length()
) will end the chain.
Returns the character found at the specified index. For example, Strung.at("abc", "b")
returns 2
.
Returns str
concatenated with the additional arguments supplied. For example, Strung.concat("a", "b", "c")
returns "abc"
.
Can also accept an array rather than a series of arguments. As in Strung.concat("a", ["b", "c"])
.
Returns a boolean indicating whether str
ends with the specified suffix
. For example, Strung.endsWith("xyz", "z")
returns true
.
Returns a boolean indicating whether str
contains the specified search
string. For example, Strung.contains("Of Mice and Men", "and")
returns true
.
An alias for Strung.contains()
. See above.
Returns the index where the specified search
string is found in str
, or 0
if it is not found. The fromIndex
indicates what index the search should start at.
For example, Strung.indexOf("alpha", "a", 3)
returns 5
.
Returns the index of the specified search
strint last occurs in str
, or 0
if it is not found.
For example, Strung.lastIndexOf("abbott", "b")
returns 3
.
Returns the result of padding the end of str
with padString
(" "
by default) until it is targetLength
long. For example, Strung.padEnd("abc", 3, "-")
returns "abc---"
.
An alias for Strung.padEnd()
see above.
Returns the result of padding the beginning of str
with padString
(" "
by default) until it is targetLength
long. For example, Strung.padStart("abc", 3, "-")
returns "---abc"
.
An alias for Strung.padStart()
see above.
Returns the result of repeating str
count
times. For example, Strung.repeat("abc", 3)
returns "abcabcabc"
.
Returns the result of replacing the first occurrence of the search
string within str
with the replacement
string.
For example, Strung.replace("alpha", "a", "x")
returns "xlpha"
.
Returns the result of replacing the all occurrences of the search
string within str
with the replacement
string.
For example, Strung.replace("alpha", "a", "x")
returns "xlphx"
.
Returns the index of the first match found by regex
within str
.
For example, Strung.search("abc123", "\d")
returns 4
.
Returns a slice of str
starting at index start
, and ending at index end
(which defaults to the length of str
).
For example, Strung.slice("abcdef", 2, 4)
returns "bcd"
.
Returns an array of strings which are the result of splitting str
by the given separator
, but only up to limit
number of strings. The mode
determines whether separator
is interpreted as a string or a regular expression. Valid values for mode
are "string"
(the default) and "regex"
.
Returns a boolean indicating whether str
starts with the specified suffix
. For example, Strung.startsWith("xyz", "x")
returns true
.
Returns the result of upper casing str
. For example, Strung.toUpperCase("abc")
returns "ABC"
.
Returns the result of lower casing str
. For example, Strung.toLowerCase("XYZ")
returns "xyz"
.
Returns the result of removing all spaces and tabs from the beginning and end of str
. For example, Strung.trim(" abc ")
returns "abc"
.
Returns the result of removing all spaces and tabs from the end of str
. For example, Strung.trimEnd(" abc ")
returns " abc"
.
Returns the result of removing all spaces and tabs from the beginning of str
. For example, Strung.trim(" abc ")
returns "abc "
.
Returns the result of uppercasing the first character of str
. For example, Strung.capitalize("john")
returns "John"
.
Returns the result of lowercasing the first character of str
. For example, Strung.capitalize("Jane")
returns "jane"
.
Returns an array of strings derived by taking every length
number of characters as a new string. The final string in the array may be less than length
if str
is not neatly divisible by length
.
For example, Strung.chop("abc", 2)
returns ["ab", "c"]
.
Returns the result of replacing all groups of spaces in str
with a single space, and trimming leading and trailing spaces. For example, Strung.consolidateSpaces(" a b c ")
returns "a b c"
.
Returns an array of the characters in str
. For example, Strung.toArray("abc")
returns ["a", "b", "c"]
.
An alias for Strung.toArray()
. See above.
Returns the result of swapping the case of each character in str
. For example, Strung.swapCase("aBc")
returns "AbC"
.
Returns the number of times search
occurs in str
. For example, Strung.count("abc", "b")
returns 1
.
Returns the length of str
. For example, Strung.length("abc")
returns 3
.
Returns the result of inserting insert
into str
at index
. For example, Strung.insert("abc", 1, "x")
returns "axbc"
.
Returns a boolean indicating whether str
is blank. A string is considered blank if it is empty, or contains only spaces and tabs. For example, Strung.isBlank(" ")
returns true
.
Returns the result of joining the given strings with the given separator
. For example, Strung.join(", ", "a", "b", "c")
returns "a, b, c"
.
Can also accept an array rather than a series of arguments. For example, Strung.join(", ", ["a", "b", "c"])
returns "a, b, c"
.
Returns an array of the lines in str
. For example, Strung.linesToArray("a\nb\nc")
returns ["a", "b", "c"]
. Lines may be delimited by either "
n"or
"r
n"`.
Returns the number of lines in str
. For example, Strung.lineCount("a\nb\nc")
returns 3
.
Returns the result of reversing the characters in str
. For example, Strung.reverse("abc")
returns "cba"
.
Returns the result of uppercasing the first character of each word in str
. For example, Strung.titleCase("the quick brown fox")
returns "The Quick Brown Fox"
.
Returns the result of truncating str
to length
characters, and appending truncateStr
if the string was truncated. For example, Strung.truncate("abcdef", 3, "...")
returns "abc..."
.
Returns an array of the words in str
. For example, Strung.wordsToArray("a b c")
returns ["a", "b", "c"]
.
Returns the number of words in str
. For example, Strung.wordCount("a b c")
returns 3
.
Returns the result of surrounding str
with surround
. For example, Strung.surround("abc", "x")
returns "xabcx"
.
Returns the result of surrounding str
with q
(which defaults to '"'
). For example, Strung.quote("abc", "'")
returns "'abc'"
.
Returns the result of removing q
(which defaults to '"'
) from the beginning and end of str
. For example, Strung.unquote("'abc'", "'")
returns "abc"
.
If there is not a q
at both the beginning and end of str
, str
is returned unchanged. For example, Strung.unquote("'abc", "'")
returns "'abc"
.
Converts str
to a boolean if it is a string commonly used as a boolean indicator. For example: Strung.toBoolean("on")
returns true
.
The following strings are considered true:
"on"
"yes"
"true"
"1"
The following strings are considered false:
"off"
"no"
"false"
"0"
Comparisons are case insensitive. For example, Strung.toBoolean("On")
returns true
.
Returns the result of applying mapper
to each character in str
. For example, Strung.map("abc", (c) => Strung.toUpperCase(c))
returns "ABC"
.