StringUtil
In Mini Micro, stringUtil is an import module in the /sys/lib directory. It provides various additional string-related functions, directly extending the string datatype.
Like all the modules in /sys/lib, the best documentation for stringUtil is the source code (/sys/lib/stringUtil.ms) itself. But this page summarizes the content in more concise form.
Constants
The following values should be normally be prefixed with stringUtil, e.g., stringUtil.smallWords.
| Name | Value / Purpose |
|---|---|
| smallWords | list of words that should not be capitalized in a title, except as the first word |
| TAB | char(9); tab character (advances to the next multiple of 4 spaces)
|
| CR | char(13); carriage return (moves the cursor to the start of the next line)
|
| LF | char(10); line feed (alternate line ending used in some files)
|
| whitespace | common whitespace characters: space, TAB, CR, and LF |
Added string methods
The following methods are added to the string type, and so are accessed using dot syntax after any string, for example: "hello world".titleCase
Note that because strings are immutable, none of these methods change the string they are called on; instead they return a new string (where applicable).
| Method | Returns |
|---|---|
| capitalized | capitalizes the first letter of the string |
| titleCase | capitalizes each word, except for small words |
| startsWith(s) | returns true if this string begins with s |
| endsWith(s) | returns true if this string ends with s |
| contains(s) | returns true if this string contains s |
| isNumeric | returns true if this string is a valid number like "-3.1415". MiniMicro Only! |
| lastIndexOf(substr, beforeIdx=null) | returns the *last* index of the given substring, optionally before a given index. Returns null if not found. |
| pad(length, padChar=" ", cutIfTooLong=true) | pads a string by appending the given pad char, optionally cutting it if too long |
| trim(charsToRemove=whitespace) | trims the given set of characters off both ends of the string |
| trimRight(charsToRemove=whitespace) | trims the given set of characters off the end of the string |
| trimLeft(charsToRemove=whitespace) | trims the given set of characters off the start of the string |
| compress(charToCompress=" ") | replaces any runs of a given character with a single instance of that character. |
| ellideEnd(maxLength=10) | shortens a string and appends an ellipsis if the string is too long |
| ellideMiddle(maxLength=10) | shortens a string in the middle, inserting an ellipsis |
| reverse | reverses the string (e.g. Hello -> olleH) |
| splitLines | splits a string into lines by CR, LF, or CR+LF |
| wrap(width=67) | splits the string on spaces so that each line fits within the given width |
| cipher(charMap) | applies a substitution cipher, using the given character map |
| rot13 | applies the classic ROT-13 substitution cipher |
| urlEncode | Encode a string so it can be used as a URL query parameter. MiniMicro Only! |
| urlDecode | convert a URL-encoded string back into plain text. MiniMicro Only! |
| between(startTag, endTag, after=null) | find a string between two substrings. This can be used, for example, to find text between two HTML tags. |
| editDistance(s2) | returns how many 1-character edits needed to transform this string into s2 |
| fill(args) | substitute values from args map into this string at places identified by "{key}". (See string.match for the inverse operation.) |
| match(pattern) | takes a pattern string that may contain fields to fill in, identified by "{key}". If the this string can be matched to that pattern, then it returns a map with the key-value pairs filled in with the corresponding parts of this string. If it can't match, then returns null. (See string.fill for the inverse operation.) |
Example
import "stringUtil"
print "hello world".titleCase.reverse // prints: dlroW olleH