Difference between revisions of "StringUtil"

From MiniScript Wiki
Jump to navigation Jump to search
m (Added to Sys Modules Category)
m
 
(2 intermediate revisions by the same user not shown)
Line 41: Line 41:
 
|-
 
|-
 
| contains(''s'') || returns true if this string contains ''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
 
| pad(''length'', ''padChar''=" ", ''cutIfTooLong''=true) || pads a string by appending the given pad char, optionally cutting it if too long
Line 49: Line 53:
 
|-
 
|-
 
| trimLeft(''charsToRemove''=whitespace) || trims the given set of characters off the start 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
 
| ellideEnd(''maxLength''=10) || shortens a string and appends an ellipsis if the string is too long
Line 64: Line 70:
 
| rot13 || applies the classic [https://en.wikipedia.org/wiki/ROT13 ROT-13] substitution cipher
 
| rot13 || applies the classic [https://en.wikipedia.org/wiki/ROT13 ROT-13] substitution cipher
 
|-
 
|-
| editDistance = function(''s2'') || returns how many 1-character edits needed to transform this string into ''s2''
+
| 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.)
 
|}
 
|}
  
Line 73: Line 89:
  
  
 +
[[Category:Sys Modules]]
 
[[Category:Mini Micro]]
 
[[Category:Mini Micro]]
[[Category:Sys Modules]]
+
[[Category:Command-Line_MiniScript]]

Latest revision as of 04:58, 5 April 2025

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