Foundations.StringUtils
This namespace contains functions that operate on strings.
| Function | Description |
| StringUtils.camelize | Takes a string with words separated by '-' and turns it into a camelCased string. |
| StringUtils.endsWith | Checks whether a string ends with a given substring. |
| StringUtils.escapeHTML | Takes a string with HTML entity forms "<", ">" and "&" and replaces them with "&lt;", "&gt;" and "&amp;". |
| StringUtils.includes | Checks whether a substring appears anywhere within a string. |
| StringUtils.isBlank | Checks whether a string is blank. A string is considered blank when it consists entirely of zero or more whitespace characters. |
| StringUtils.parseQueryString | Turns a string containing name/value pairs into an object containing name/value pairs. |
| StringUtils.startsWith | Checks whether a string starts with a given substring. |
| StringUtils.stripScripts | From a string, removes script tags and anything inside them. |
| StringUtils.stripTags | Removes all HTML tags from a string and returns the result. |
| StringUtils.unescapeHTML | Unescapes a string with HTML entity forms "&lt;", "&gt;" and "&amp;" and replaces them with "<", ">" and "&". |
StringUtils.camelize()
Takes a string with words separated by '-' and turns it into a camelCased string. For example:
"some-string" becomes "someString" "some-other-string" becomes "someOtherString"
Syntax
string StringUtils.camelize(str)
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to camelize. |
Returns
{ str : string }
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var notCamelStr = "string-to-camelize"; Mojo.Log.info("camelStr = "+ JSON.stringify(StringUtils.camelize(notCamelStr)));
Example Output
camelStr = "stringToCamelize"
StringUtils.endsWith()
Checks whether a string ends with a given substring.
Syntax
boolean StringUtils.endsWith(str, subStr)
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to check. |
| subStr | Yes | string | Substring to search for at end. |
Returns
true or false
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var str = "stringToCheck"; var subStr = "Check"; Mojo.Log.info("endsWith = "+ StringUtils.endsWith(str, subStr));
Example Output
endsWith = true
StringUtils.escapeHTML()
Takes a string with HTML entity forms "<", ">" and "&" and replaces them with "<", ">" and "&".
Syntax
string StringUtils.escapeHTML(escStr)
Parameters
| Argument | Required | Type | Description |
| escStr | Yes | string | String to escape. |
Returns
escaped string
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var escStr = "string>To<Escape&"; Mojo.Log.info("escapeHTML = "+ StringUtils.escapeHTML(escStr));
Example Output
escapeHTML = string>To<Escape&
StringUtils.includes()
Checks whether a substring appears anywhere within a string.
Syntax
boolean StringUtils.includes(str, subStr)
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to check. |
| subStr | Yes | string | Substring to check for. |
Returns
true or false
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var strInc = "StringToCheckForSubstring"; var subStrInc = "For"; Mojo.Log.info("inlcudes = "+ StringUtils.includes(strInc, subStrInc));
Example Output
includes = true
StringUtils.isBlank()
Checks whether a string is blank. A string is considered blank when it consists entirely of zero or more whitespace characters.
Syntax
boolean StringUtils.isBlank(str)
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to check for blankness. |
Returns
true or false
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var blankStr = " "; Mojo.Log.info("isBlank = "+ StringUtils.isBlank(blankStr));
Example Output
isBlank = true
StringUtils.parseQueryString()
Takes a string of the form:
key1=value%20one&key2=val2
And turns it into an object:
{"key1":"value one","key2":"val2"}
It replaces the Prototye function String#toQueryParams and its alias String#parseQuery.
Syntax
object StringUtils.parseQueryString(string)
Parameters
| Argument | Required | Type | Description |
| string | Yes | string | String to convert to object. |
Returns
{ object : any }
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var itemPriceStr = "Item=drink&price=2"; Mojo.Log.info("ItemPrice obj = "+ JSON.stringify(StringUtils.parseQueryString(itemPriceStr)));
Example Output
ItemPrice obj = {"Item":"drink","price":"2"}
StringUtils.startsWith()
Checks whether a string starts with a given substring. It can be thought of as another way of saying:
str.indexOf(substring) === 0
Syntax
boolean StringUtils.startsWith(str, subStr)
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to check. |
| subStr | Yes | string | Substring to search for at beginning. |
Returns
true or false
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var str = "stringToCheck"; var subStr = "str"; Mojo.Log.info("startsWith = "+ StringUtils.startsWith(str, subStr));
Example Output
startsWith = true
StringUtils.stripScripts()
From a string, removes <script> tags and anything inside them.
Syntax
string StringUtils.stripScripts(str);
Parameters
| Argument | Required | Type | Description |
| str | Yes | string | String to strip. |
Returns
{ strippedString : string }
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var scriptsStr = "Remove <script>Please remove me</script>this"; Mojo.Log.info("strippedScriptsStr = "+ StringUtils.stripScripts(scriptsStr));
Example Output
strippedScriptsStr = Remove this
StringUtils.stripTags()
This function removes all HTML tags from a string and returns the result.
Syntax
string StringUtils.stripTags(tagsStr);
Parameters
| Argument | Required | Type | Description |
| tagsStr | Yes | string | String to strip. |
Returns
{ strippedString : string }
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" }); var StringUtils = libraries["foundations"].StringUtils; var tagsStr = "<table><tr><td>Let the good times roll</td></tr></table>"; Mojo.Log.info("strippedTagsStr = "+ StringUtils.stripTags(tagsStr));
Example Output
strippedTagsStr = Let the good times roll
StringUtils.unescapeHTML()
Unescapes a string with HTML entity forms "<", ">" and "&" and replaces them with "<", ">" and "&".
Syntax
StringUtils.unescapeHTML(unescStr)
Parameters
| Argument | Required | Type | Description |
| unescStr | Yes | string | String to unescape. |
Returns
true or false
Example
var libraries = MojoLoader.require({ name: "foundations", version: "1.0" });
var StringUtils = libraries["foundations"].StringUtils;
var unescStr = "string>To<unescape&";
Mojo.Log.info("unescapeHTML = "+ StringUtils.unescapeHTML(unescStr));
Example Output
unescapeHTML = string>To<unescape&