Class o2.String
static
class
o2.String
Replaces HTML [br /] tags with new line.
Usage example:
var replaced = o2.String.br2nl('hello
world.');
Works identical to String.trim(str,
true)
.
Usage example:
var compacted = o2.String.compact(' lorem ipsum ');
Concatanes all its arguments into a single String
.
This is faster than adding those String
s with
+
.
Usage example:
var joined = o2.String.concat('lorem', ipsum);
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);
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);
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);
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"
Creates a globally unique identifier (i.e. GUID), for that browsing session.
Usage example:
var guid = o2.String.generateGuid();
Generates a random String
.
Usage example:
var rnd = o2.String.generateRandom();
An alias to o2.String.encode.
Replaces new lines [\n] with HTML [br /] tags.
Usage example:
var replaced = o2.String.nl2br('hello\nworld.');
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'});
Simply removes the phrases that match the RegExp
from
the String
.
Usage example:
var removed = o2.String.remove('lorem', /e/ig);
An alias to o2.String.encodeSafeHtml.
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.!');
Removes non-numeric characters from the String
.
Usage example:
var stripped = o2.String.stripNonNumeric('abc123.!');
Removes numeric characters from the String
.
Usage example:
var stripped = o2.String.stripNumeric('abc123.!');
Removes tags from the String
.
Usage example:
var stripped = o2.String.stripTags('abc123.!
');
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');
Converts a String
of the form 'loremIpsum' to
'lorem-ipsum'.
Usage example:
var dashed = o2.String.toDashedFromCamelCase('fontFamily');
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');
Trims white space from beginning and end of the
String
.
Usage example:
var trimmed = o2.String.trim(' lorem ');
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.');
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.');
str
- the String
to format. String
.
function compact
static
compact(String
str)
Works identical to String.trim(str,
true)
.
Usage example:
var compacted = o2.String.compact(' lorem ipsum ');
str
- the String
to process. String
.
function concat
static
concat()
Concatanes all its arguments into a single String
.
This is faster than adding those String
s with
+
.
Usage example:
var joined = o2.String.concat('lorem', ipsum);
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);
str
- the String
to process. 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);
str
- the String
to process. 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);
str
- the String
to process. String
.
function escape
static
escape(String
str)
An alias to encodeURIComponent
.
str
- the String
to process. 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"
String
.
function generateGuid
static
generateGuid()
Creates a globally unique identifier (i.e. GUID), for that browsing session.
Usage example:
var guid = o2.String.generateGuid();
function generateRandom
static
generateRandom(Integer
length)
Generates a random String
.
Usage example:
var rnd = o2.String.generateRandom();
length
- (optional - default: String.config.constants.DEFAULT_RANDOM_LENGTH)
length of the String
to be generated. String
.
function htmlEncode
static
htmlEncode()
An alias to o2.String.encode.
function nl2br
static
nl2br(String
str)
Replaces new lines [\n] with HTML [br /] tags.
Usage example:
var replaced = o2.String.nl2br('hello\nworld.');
str
- the String
to format. 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'});
str
- the String
to format. 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);
str
- the String
to process.
regExp
- the RegExp
to process against. String
.
function safeHtmlEncode
static
safeHtmlEncode()
An alias to o2.String.encodeSafeHtml.
function stripNonAlpha
static
stripNonAlpha(String
str)
Removes non alphabetical characters from the String
(excluding numbers).
Usage example:
var stripped = o2.String.stripNonAlpha('abc123.!');
str
- the String
to format. String
.
function stripNonAlphanumeric
static
stripNonAlphanumeric(String
str)
Removes alpha-numeric characters from the String
.
Usage example:
var stripped = o2.String.stripNonAlphanumeric('abc123.!');
str
- the String
to format. String
.
function stripNonNumeric
static
stripNonNumeric(String
str)
Removes non-numeric characters from the String
.
Usage example:
var stripped = o2.String.stripNonNumeric('abc123.!');
str
- the String
to format.
- String
.
function stripNumeric
static
stripNumeric(String
str)
Removes numeric characters from the String
.
Usage example:
var stripped = o2.String.stripNumeric('abc123.!');
str
- the String
to format. String
.
function stripTags
static
stripTags(String
str)
Removes tags from the String
.
Usage example:
var stripped = o2.String.stripTags('abc123.!
');
str
- the String
to format. 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');
input
- the String
to convert. function toDashedFromCamelCase
static
toDashedFromCamelCase(String
input)
Converts a String
of the form 'loremIpsum' to
'lorem-ipsum'.
Usage example:
var dashed = o2.String.toDashedFromCamelCase('fontFamily');
input
- the String
to convert. String
.
function toJson
static
toJson(String
str)
Converts the given String
to a JSON
object.
Usage example:
var parsed = o2.String.toJson('{"name" : "value"}');
str
- the String
to convert. Object
.
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');
input
- the String
to convert. 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 ');
str
- the String
to process.
shouldCompact
- Optional (default:
false
)
if true
, multiple whitespace is compacted into single
whitespace. 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.');
str
- the String
to process.
maxLen
- Optional (defaults TRUNCATION_LENGTH},
maximum String
length that's allowed without truncation. String
.
function unescape
static
unescape(String
str)
An alias to decodeURIComponent
.
str
- the String
to process. 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);
str
- the String
to process
isAmpersandsPreserved
- (Optional. Defaults to
false
). If true
& characters will not be
encoded, otherwise they will be. String
.
A
String
helper class.