All samples are using Ink.requireModules, please read how to use it at Ink.requireModules section

Ink.Util.String_1 class

Functions
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.
Properties
Property name Description
.escapedCharRegex Regex to check escaped strings

.escape(c) method

Escapes a unicode character.

Accepts

  • c

    Character to escape

Code

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
});

.escapeText(txt, [whiteList]) method

Escapes unicode characters in a string as unicode character entities (\x##, where the ## are hex digits).

Accepts

  • txt

    String with characters outside the ASCII printable range (32 < charCode < 127)
  • whiteList

    Whitelist of characters which should NOT be escaped

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.escapeText('some áudío çapo');
    Ink.log(result1); // some \xe1ud\xedo \xe7apo 
});

.evalJSON(strJSON, sanitize) method

Eval a JSON - We recommend you Ink.Util.Json

Accepts

  • strJSON

    JSON string to eval
  • sanitize

    Flag to sanitize input

.htmlEntitiesDecode(string) method

Decodes string from HTML entities.

Accepts

  • string

    String to be decoded

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.htmlEntitiesDecode('&lt;p&gt;text with a &lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/p&gt;'); 
    Ink.log(result1); // <p>text sample with a <a href="#">link</a></p> 
});

.htmlEntitiesEncode(string) method

Encodes string into HTML entities.

Accepts

  • 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); // &lt;p&gt;text with a &lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/p&gt; 
});

.htmlEscapeUnsafe(str) method

Escapes unsafe html chars as HTML entities

Accepts

  • str

    String to escape

Code

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 &lt;span&gt;sample&lt;/span&gt; text &amp; with &quot;more&quot; text 
});

.isJSON(str) method

Checks if a string is a valid JSON object (string encoded)

Accepts

  • str

    String to check

.normalizeWhitespace(str) method

Normalizes whitespace in string.
String is trimmed and sequences of whitespaces are collapsed.

Accepts

  • str

    String to normalize

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.normalizeWhitespace('this a     sample text   ');
    Ink.log(result1); // this a sample text 
});

.packetize(str, maxLen) method

Splits a string into smaller chunks

Accepts

  • str

    String to divide
  • maxLen

    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"]
});

.removeAccentedChars(string) method

Removes all accented characters from a string.

Accepts

  • string

    String to remove accents from

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.removeAccentedChars('águeda, bragança, setúbal');
    Ink.log(result1); // agueda, braganca, setubal 
});

.shortString(str, n) method

Truncates a string without breaking words. Inserts an ellipsis HTML entity at the end of the string if it's too long.

Accepts

  • str

    String to truncate
  • n

    Number of chars of the short string

Code

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 &hellip;
});

.strcmp(str1, str2) method

Compares two strings.

Accepts

  • str1

    First String
  • str2

    Second String

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    InkString.strcmp('aa', 'aa'); // 0 
    InkString.strcmp('aa', 'bb'); // -1 
    InkString.strcmp('bb', 'aa'); // 1 
});

.stripTags(string, allowed) method

Strips HTML tags from strings

Accepts

  • 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>
});

.substrCount(haystack, needle) method

Count the number of occurrences of a specific needle in a haystack

Accepts

  • haystack

    String to search in
  • needle

    String to search for

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.substrCount('this a sample text with more text', 'text');
    Ink.log(result1); // 2 
});

.toUnicode(str) method

Converts string to unicode.

Accepts

  • str

    String to convert

Code

Ink.requireModules(['Ink.Util.String_1'], function(InkString) {

    var result1 = InkString.toUnicode('some áudío çapo');
    Ink.log(result1); // some \u00e1ud\u00edo \u00e7apo 
});

.trim(string) method

Trims whitespace from strings

Accepts

  • string

    String to be trimmed

Code

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| 
});

.truncateString(str, length) method

Truncates a string, breaking words and adding ... at the end.

Accepts

  • str

    String to truncate
  • length

    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… 
});

.ucFirst(string, [firstWordOnly]) method

Capitalizes a word.
If param as more than one word, it converts first letter of all words that have more than 2 letters

Accepts

  • 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 
});

.unescape(es) method

Unescapes a unicode character escape sequence

Accepts

  • es

    Escape sequence

Code

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); // ç 
});

.unescapeText(txt) method

Removes unicode entities (in the format "\x##" or "\u####", where "#" is a hexadecimal digit)

Accepts

  • 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 
});

.utf8Decode(string) method

Decodes a string from UTF-8.

Accepts

  • string

    String to be decoded

.utf8Encode(string) method

Encode a string to UTF-8.

Accepts

  • string

    String to be encoded

Ink.Util.String_1.escapedCharRegex attribute

Regex

Regex to check escaped strings