JAVASCRIPT
String Utilities
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
Function name | Description |
---|---|
.escape(c) | Escapes a unicode character. |
.escapeText(txt, [whiteList]) | Escapes unicode characters in a string as unicode character entities (\x## , where the ## are hex digits). |
.evalJSON(strJSON, sanitize) | Eval a JSON - We recommend you Ink.Util.Json |
.htmlEntitiesDecode(string) | Decodes string from HTML entities. |
.htmlEntitiesEncode(string) | Encodes string into HTML entities. |
.htmlEscapeUnsafe(str) | Escapes unsafe html chars as HTML entities |
.isJSON(str) | Checks if a string is a valid JSON object (string encoded) |
.normalizeWhitespace(str) | Normalizes whitespace in string. |
.packetize(str, maxLen) | Splits a string into smaller chunks |
.removeAccentedChars(string) | Removes all accented characters from a string. |
.shortString(str, n) | Truncates a string without breaking words. Inserts an ellipsis HTML entity at the end of the string if it's too long. |
.strcmp(str1, str2) | Compares two strings. |
.stripTags(string, allowed) | Strips HTML tags from strings |
.substrCount(haystack, needle) | Count the number of occurrences of a specific needle in a haystack |
.toUnicode(str) | Converts string to unicode. |
.trim(string) | Trims whitespace from strings |
.truncateString(str, length) | Truncates a string, breaking words and adding ... at the end. |
.ucFirst(string, [firstWordOnly]) | Capitalizes a word. |
.unescape(es) | Unescapes a unicode character escape sequence |
.unescapeText(txt) | Removes unicode entities (in the format "\x##" or "\u####", where "#" is a hexadecimal digit) |
.utf8Decode(string) | Decodes a string from UTF-8. |
.utf8Encode(string) | Encode a string to UTF-8. |
Property name | Description |
---|---|
.escapedCharRegex | Regex to check escaped strings |
Escapes a unicode character.
c
Character to escapeCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.escape('c');
Ink.log(result1); // \x63
var result2 = InkString.escape('ç');
Ink.log(result2); // \xE7
});
Escapes unicode characters in a string as unicode character entities (\x##
, where the ##
are hex digits).
txt
String with characters outside the ASCII printable range (32 < charCode < 127)whiteList
Whitelist of characters which should NOT be escapedCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.escapeText('some áudío çapo');
Ink.log(result1); // some \xe1ud\xedo \xe7apo
});
Eval a JSON - We recommend you Ink.Util.Json
strJSON
JSON string to evalsanitize
Flag to sanitize inputDecodes string from HTML entities.
string
String to be decodedCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.htmlEntitiesDecode('<p>text with a <a href="#">link</a></p>');
Ink.log(result1); // <p>text sample with a <a href="#">link</a></p>
});
Encodes string into HTML entities.
string
Input string.Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.htmlEntitiesEncode('<p>text sample with a <a href="#">link</a></p>');
Ink.log(result1); // <p>text with a <a href="#">link</a></p>
});
Escapes unsafe html chars as HTML entities
str
String to escapeCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.htmlEscapeUnsafe('this a <span>sample</span> text & with "more" text');
Ink.log(result1); // this a <span>sample</span> text & with "more" text
});
Checks if a string is a valid JSON object (string encoded)
str
String to checkNormalizes whitespace in string.
String is trimmed and sequences of whitespaces are collapsed.
str
String to normalizeCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.normalizeWhitespace('this a sample text ');
Ink.log(result1); // this a sample text
});
Splits a string into smaller chunks
str
String to dividemaxLen
Maximum chunk size (in characters)Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.packetize('some text to packetize', 3);
Ink.log(result1); // ["som", "e t", "ext", " to", " pa", "cke", "tiz", "e"]
});
Removes all accented characters from a string.
string
String to remove accents fromCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.removeAccentedChars('águeda, bragança, setúbal');
Ink.log(result1); // agueda, braganca, setubal
});
Truncates a string without breaking words. Inserts an ellipsis HTML entity at the end of the string if it's too long.
str
String to truncaten
Number of chars of the short stringCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.shortString('this a sample text to truncate', 15);
Ink.log(result1); // this a sample …
});
Compares two strings.
str1
First Stringstr2
Second StringCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
InkString.strcmp('aa', 'aa'); // 0
InkString.strcmp('aa', 'bb'); // -1
InkString.strcmp('bb', 'aa'); // 1
});
Strips HTML tags from strings
string
String to strip tags from.allowed
Comma separated list of allowed tags.Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.stripTags('<p>text <span>sample</span> with a <a href="#">link</a></p>');
Ink.log(result1); // text sample with a link
var result2 = InkString.stripTags('<p>text <span>sample</span> with a <a href="#">link</a></p>', 'a,span');
Ink.log(result2); // text <span>sample</span> with a <a href="#">link</a>
});
Count the number of occurrences of a specific needle in a haystack
haystack
String to search inneedle
String to search forCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.substrCount('this a sample text with more text', 'text');
Ink.log(result1); // 2
});
Converts string to unicode.
str
String to convertCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.toUnicode('some áudío çapo');
Ink.log(result1); // some \u00e1ud\u00edo \u00e7apo
});
Trims whitespace from strings
string
String to be trimmedCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.trim(' this is a text sample ');
Ink.log('|'+result1+'|'); // |this is a text sample|
});
Truncates a string, breaking words and adding ... at the end.
str
String to truncatelength
Limit for the returned string, ellipsis included.Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.truncateString('this a sample text', 10);
Ink.log(result1); // this a sa…
});
Capitalizes a word.
If param as more than one word, it converts first letter of all words that have more than 2 letters
string
String to capitalize.firstWordOnly
falseFlag to capitalize only the first word.Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.ucFirst('text sample');
Ink.log(result1); // Text Sample
var result2 = InkString.ucFirst('text sample', true);
Ink.log(result2); // Text sample
});
Unescapes a unicode character escape sequence
es
Escape sequenceCode
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.unescape('\x000063');
Ink.log(result1); // c
var result2 = InkString.unescape('\x0000E7');
Ink.log(result2); // ç
});
Removes unicode entities (in the format "\x##" or "\u####", where "#" is a hexadecimal digit)
txt
Text you intend to remove unicode character entities.Code
Ink.requireModules(['Ink.Util.String_1'], function(InkString) {
var result1 = InkString.unescapeText('some \xe1ud\xedo \xe7apo');
Ink.log(result1); // some áudío çapo
});
Decodes a string from UTF-8.
string
String to be decodedEncode a string to UTF-8.
string
String to be encodedRegex
Regex to check escaped strings