StringUtil

From MiniScript Wiki
Jump to navigation Jump to search

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
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
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
editDistance = function(s2) returns how many 1-character edits needed to transform this string into s2

Example

import "stringUtil"
print "hello world".titleCase.reverse  // prints: dlroW olleH