Class o2.String


static class o2.String

A String helper class.

Defined in string.core

Function Summary
static br2nl (String str)

Replaces HTML [br /] tags with new line.

Usage example:

 var replaced = o2.String.br2nl('hello
world.');
static compact (String str)

Works identical to String.trim(str, true).

Usage example:

 var compacted = o2.String.compact('   lorem      ipsum     ');
 
static concat()

Concatanes all its arguments into a single String. This is faster than adding those Strings with +.

Usage example:

 var joined = o2.String.concat('lorem', ipsum);
 
static decode (String str)

Decodes HTML entities back to normal characters.

If possible try using standard decoding methods like decodeURIComponent, instead of using this method.

Usage example:

 var decoded = o2.String.decode(encodedString);
 
static encode (String str)

Encodes special charaters to their corresponding HTML entities.

If possible try using standard encoding methods like encodeURIComponent, instead of using this method.

Usage example:

 var encoded = o2.String.decode(inputString);
 
static encodeSafeHtml (String str)

Works similar to o2.String.encode.

Encodes the String by converting it into a text node and returning the node's value.

Usage example:

 var encoded = o2.String.encodeSafeHtml(inputString);
 
static escape (String str)

An alias to encodeURIComponent.

static format()

Works similar to C#'s String.Format.

Usage example:

 o2.String.format("Hello {0}. What's going on in {1}?", 'Ninja',
 'California');
 //will return "Hello Ninja. What's going on in California"
 
static generateGuid()

Creates a globally unique identifier (i.e. GUID), for that browsing session.

Usage example:

 var guid = o2.String.generateGuid();
 
static generateRandom (Integer length)

Generates a random String.

Usage example:

 var rnd = o2.String.generateRandom();
 
static htmlEncode()

An alias to o2.String.encode.

static nl2br (String str)

Replaces new lines [\n] with HTML [br /] tags.

Usage example:

 var replaced = o2.String.nl2br('hello\nworld.');
 
static printf (String str)

Works similar to C's printf function.

Usage example:

 var test1 = 'lorem %s %s sit amet';
 var test2 = 'lorem %1:s %2:s sit %2:s amet %1:s';
 var test3 = 'lorem %id:s ipsum';

 //This will return 'lorem ipsum dolor sit amet''
 o2.String.printf(test1, 'ipsum', 'dolor');

 //This will return 'lorem ipsum dolor sit dolor amet ipsum'
 o2.String.printf(test1, 'ipsum', 'dolor');

 //This will return 'lorem test ipsum'.
 o2.String.printf(test3, {id : 'test'});
 
static remove (String str, RegExp regExp)

Simply removes the phrases that match the RegExp from the String.

Usage example:

 var removed = o2.String.remove('lorem', /e/ig);
 
static stripNonAlpha (String str)

Removes non alphabetical characters from the String (excluding numbers).

Usage example:

 var stripped = o2.String.stripNonAlpha('abc123.!');
 

Removes alpha-numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNonAlphanumeric('abc123.!');
 
static stripNonNumeric (String str)

Removes non-numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNonNumeric('abc123.!');
 
static stripNumeric (String str)

Removes numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNumeric('abc123.!');
 
static stripTags (String str)

Removes tags from the String.

Usage example:

 var stripped = o2.String.stripTags('

abc123.!

');
static toCamelCase (String input)

Converts the input to camel case.

i.e. if input is 'lorem-ipsum', the output is 'loremIpsum'.

This is especially useful for converting CSS classes to their DOM style representations.

Usage example:

 var camelized = o2.String.toCamelCase('font-family');
 
static toDashedFromCamelCase (String input)

Converts a String of the form 'loremIpsum' to 'lorem-ipsum'.

Usage example:

 var dashed = o2.String.toDashedFromCamelCase('fontFamily');
 
static toJson (String str)

Converts the given String to a JSON object.

Usage example:

 var parsed = o2.String.toJson('{"name" : "value"}');
 

Converts a String of the form 'loremIpsum' to 'lorem_ipsum'.

Usage example:

 var replaced = o2.String.toUnderscoreFromCamelCase('fontFamily');
 
static trim (String str, Boolean shouldCompact)

Trims white space from beginning and end of the String.

Usage example:

 var trimmed = o2.String.trim('    lorem     ');
 
static truncate (String str, Integer maxLen)

Adds an ellipsis (…), if the length of the String is greater than maxLen.

Usage example:

 var truncated = o2.String.truncate('This ... is a very long String.');
 
static unescape (String str)

An alias to decodeURIComponent.

static xssEncode (String str, Boolean isAmpersandsPreserved)

Encodes special charaters to their corresponding HTML entities. Works similar to {link String.encode}, with an exception that it does not encode whitespace characters.

This method is specially designed to prevent cross-site script injection attacks.

Usage example:

 var encoded = o2.String.xssEncode(inputString);
 

Function Details

function br2nl

static br2nl(String str)

Replaces HTML [br /] tags with new line.

Usage example:

 var replaced = o2.String.br2nl('hello
world.');
Parameters:
str - the String to format.
Returns:
the formatted String.

function compact

static compact(String str)

Works identical to String.trim(str, true).

Usage example:

 var compacted = o2.String.compact('   lorem      ipsum     ');
 
Parameters:
str - the String to process.
Returns:
the processed String.
See also:
String.trim

function concat

static concat()

Concatanes all its arguments into a single String. This is faster than adding those Strings with +.

Usage example:

 var joined = o2.String.concat('lorem', ipsum);
 
Returns:
the concataneted String.

function decode

static decode(String str)

Decodes HTML entities back to normal characters.

If possible try using standard decoding methods like decodeURIComponent, instead of using this method.

Usage example:

 var decoded = o2.String.decode(encodedString);
 
Parameters:
str - the String to process.
Returns:
the processed String.

function encode

static encode(String str)

Encodes special charaters to their corresponding HTML entities.

If possible try using standard encoding methods like encodeURIComponent, instead of using this method.

Usage example:

 var encoded = o2.String.decode(inputString);
 
Parameters:
str - the String to process.
Returns:
the processed String.

function encodeSafeHtml

static encodeSafeHtml(String str)

Works similar to o2.String.encode.

Encodes the String by converting it into a text node and returning the node's value.

Usage example:

 var encoded = o2.String.encodeSafeHtml(inputString);
 
Parameters:
str - the String to process.
Returns:
the processed String.
See also:

function escape

static escape(String str)

An alias to encodeURIComponent.

Parameters:
str - the String to process.
Returns:
the processed String.

function format

static format()

Works similar to C#'s String.Format.

Usage example:

 o2.String.format("Hello {0}. What's going on in {1}?", 'Ninja',
 'California');
 //will return "Hello Ninja. What's going on in California"
 
Returns:
the formated String.

function generateGuid

static generateGuid()

Creates a globally unique identifier (i.e. GUID), for that browsing session.

Usage example:

 var guid = o2.String.generateGuid();
 
Returns:
a GUID.

function generateRandom

static generateRandom(Integer length)

Generates a random String.

Usage example:

 var rnd = o2.String.generateRandom();
 
Parameters:
length - (optional - default: String.config.constants.DEFAULT_RANDOM_LENGTH) length of the String to be generated.
Returns:
the generated String.

function htmlEncode

static htmlEncode()

An alias to o2.String.encode.

See also:

function nl2br

static nl2br(String str)

Replaces new lines [\n] with HTML [br /] tags.

Usage example:

 var replaced = o2.String.nl2br('hello\nworld.');
 
Parameters:
str - the String to format.
Returns:
the formatted String.

function printf

static printf(String str)

Works similar to C's printf function.

Usage example:

 var test1 = 'lorem %s %s sit amet';
 var test2 = 'lorem %1:s %2:s sit %2:s amet %1:s';
 var test3 = 'lorem %id:s ipsum';

 //This will return 'lorem ipsum dolor sit amet''
 o2.String.printf(test1, 'ipsum', 'dolor');

 //This will return 'lorem ipsum dolor sit dolor amet ipsum'
 o2.String.printf(test1, 'ipsum', 'dolor');

 //This will return 'lorem test ipsum'.
 o2.String.printf(test3, {id : 'test'});
 
Parameters:
str - the String to format.
Returns:
the formatted String.

function remove

static remove(String str, RegExp regExp)

Simply removes the phrases that match the RegExp from the String.

Usage example:

 var removed = o2.String.remove('lorem', /e/ig);
 
Parameters:
str - the String to process.
regExp - the RegExp to process against.
Returns:
the processed String.

function safeHtmlEncode

static safeHtmlEncode()

function stripNonAlpha

static stripNonAlpha(String str)

Removes non alphabetical characters from the String (excluding numbers).

Usage example:

 var stripped = o2.String.stripNonAlpha('abc123.!');
 
Parameters:
str - the String to format.
Returns:
the formatted String.

function stripNonAlphanumeric

static stripNonAlphanumeric(String str)

Removes alpha-numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNonAlphanumeric('abc123.!');
 
Parameters:
str - the String to format.
Returns:
the formatted String.

function stripNonNumeric

static stripNonNumeric(String str)

Removes non-numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNonNumeric('abc123.!');
 
Parameters:
str - the String to format. -
Returns:
the formatted String.

function stripNumeric

static stripNumeric(String str)

Removes numeric characters from the String.

Usage example:

 var stripped = o2.String.stripNumeric('abc123.!');
 
Parameters:
str - the String to format.
Returns:
the formatted String.

function stripTags

static stripTags(String str)

Removes tags from the String.

Usage example:

 var stripped = o2.String.stripTags('

abc123.!

');
Parameters:
str - the String to format.
Returns:
the formatted String.

function toCamelCase

static toCamelCase(String input)

Converts the input to camel case.

i.e. if input is 'lorem-ipsum', the output is 'loremIpsum'.

This is especially useful for converting CSS classes to their DOM style representations.

Usage example:

 var camelized = o2.String.toCamelCase('font-family');
 
Parameters:
input - the String to convert.
Returns:
the formatted String.

function toDashedFromCamelCase

static toDashedFromCamelCase(String input)

Converts a String of the form 'loremIpsum' to 'lorem-ipsum'.

Usage example:

 var dashed = o2.String.toDashedFromCamelCase('fontFamily');
 
Parameters:
input - the String to convert.
Returns:
the formatted String.

function toJson

static toJson(String str)

Converts the given String to a JSON object.

Usage example:

 var parsed = o2.String.toJson('{"name" : "value"}');
 
Parameters:
str - the String to convert.
Returns:
the converted JSON Object.
Throws:
Exception - if str is not a well-formed JSON String.

function toUnderscoreFromCamelCase

static toUnderscoreFromCamelCase(String input)

Converts a String of the form 'loremIpsum' to 'lorem_ipsum'.

Usage example:

 var replaced = o2.String.toUnderscoreFromCamelCase('fontFamily');
 
Parameters:
input - the String to convert.
Returns:
the formatted String.

function trim

static trim(String str, Boolean shouldCompact)

Trims white space from beginning and end of the String.

Usage example:

 var trimmed = o2.String.trim('    lorem     ');
 
Parameters:
str - the String to process.
shouldCompact - Optional (default: false) if true, multiple whitespace is compacted into single whitespace.
Returns:
the processed String.

function truncate

static truncate(String str, Integer maxLen)

Adds an ellipsis (…), if the length of the String is greater than maxLen.

Usage example:

 var truncated = o2.String.truncate('This ... is a very long String.');
 
Parameters:
str - the String to process.
maxLen - Optional (defaults TRUNCATION_LENGTH}, maximum String length that's allowed without truncation.
Returns:
the processed String.

function unescape

static unescape(String str)

An alias to decodeURIComponent.

Parameters:
str - the String to process.
Returns:
the processed String.

function xssEncode

static xssEncode(String str, Boolean isAmpersandsPreserved)

Encodes special charaters to their corresponding HTML entities. Works similar to {link String.encode}, with an exception that it does not encode whitespace characters.

This method is specially designed to prevent cross-site script injection attacks.

Usage example:

 var encoded = o2.String.xssEncode(inputString);
 
Parameters:
str - the String to process
isAmpersandsPreserved - (Optional. Defaults to false). If true & characters will not be encoded, otherwise they will be.
Returns:
the processed String.