String (object extension)

Categories:Objects

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

Parameters:

  • length Number An optional length, defaults to 6
  • chr String An optional padding character, defaults to 0
  • right Boolean An optional flag to pad to the right, rather than the left

Returns:

  •   String The padded value

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

Parameters:

  • num Number The number of times to repeat the string

The following example repeats a character 10 times:

trace('.'.repeat(10));
..........

inject(obj)

Injects a template string with values

Parameters:

  • obj Object Any Object or instance of Class that with named properties
  • obj Array An Array of values, which replace named {placeholders} in the order they are found
  • ...rest Arguments Separaten arguments, which replace named {placeholders} in the order they are found

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

Parameters:

  • capitalise Boolean An optional Boolean to Capitalise the camelCased string

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());
&lt;xml&gt;hello world&lt;/xml&gt;

unescapeHTML()

Unescapes a string of HTML entities to a valid HTML string

The following example unescapes some escaped XML:

'&lt;xml&gt;hello world&lt;/xml&gt;'.unescapeHTML();
<xml>hello world</xml>

Comments are closed.