String (object extension)
Overview
Summary
Additional functionality for native String object
Contents
Concept
The String ...
Usage
The String ...
API
Modify content
trim()
Trims the whitespace from both sides of a string
The following example trims the empty space from the ends of a string:
Utils.trim(' Hello world \n\t');
Hello world
pad(length, chr, right)
Pads a value to a certain length with a specific character
The following example pads a string to the left:
trace('hello world'.pad(20, ' '));
hello world
The following example uses the default parameters to create a properly-formatted Hex string::
trace((255).toString(16).pad());
0000FF
repeat(num)
Repeat a string a specified number of times
The following example repeats a character 10 times:
trace('.'.repeat(10));
..........
inject(obj)
Injects a template string with values
The following examples all inject values into a string of text with placeholder variables:
var template = '{greeting} {object}'; var obj = {greeting:'hello', object:'world'}; var str = template.inject(obj); // Object var str = template.inject('hello', 'world'); // Arguments var str = template.inject(['hello', 'world']); // Array trace(str);
hello world
Change case
toCamelCase(capitalise)
camelCase a string or variable name, separating on alpha-numeric characters
The following example converts text to camelCase:
trace('hello world'.toCamelCase());
helloWorld
fromCamelCase()
Convert a value from "camelCase" to "separate words"
The following example converts a camelCase variable to separate words:
trace('helloWorld'.fromCamelCase());
hello world
toSentenceCase()
Converts a the string to sentense case, by capitalising the first letter
The following example converts text to sentence case:
trace('hello world'.toSentenceCase());
Hello world
toUnderscore()
Converts a string of words (separated by non-word characters, such as spaces or dashes) to underscore_case
The following example converts text to underscore format:
trace('hello world'.toUnderscore());
hello_world
Escape
escapeHTML()
Escapes an HTML string using HTML entities
The following example escapes some XML:
trace('<xml>hello world</xml>'.escapeHTML());
<xml>hello world</xml>
unescapeHTML()
Unescapes a string of HTML entities to a valid HTML string
The following example unescapes some escaped XML:
'<xml>hello world</xml>'.unescapeHTML();
<xml>hello world</xml>
Comments are closed.