Clear Up
SharpKit Reference

String Class

A collection of useful static methods to deal with strings

Namespace: Ext

Base Types




Name Description
addCharacterEntities(object) Adds a set of character entity definitions to the set used by htmlEncode and htmlDecode. This object should be keyed by the entity name sequence, with the value being the textual representation of the entity.
            var s = Ext.String.htmlEncode("A string with entities: èÜçñ");
Note: the values of the character entites defined on this object are expected to be single character values. As such, the actual values represented by the characters are sensitive to the character encoding of the javascript source file when defined in string literal form. Script tasgs referencing server resources with character entities must ensure that the 'charset' attribute of the script node is consistent with the actual character encoding of the server resource. The set of character entities may be reset back to the default state by using the resetCharacterEntities method
capitalize(JsString) Capitalize the given string
createVarName(JsString) Converts a string of characters into a legal, parseable Javascript var name as long as the passed string contains at least one alphabetic character. Non alphanumeric characters, and leading non alphabetic characters will be removed.
ellipsis(JsString, JsNumber, bool) Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
escape(JsString) Escapes the passed string for ' and \
escapeRegex(JsString) Escapes the passed string for use in a regular expression
format(JsString, JsString, JsString) Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
var cls = 'my-class', text = 'Some text';
        var s = Ext.String.format('<div class="{0}">{1}</div>', cls, text);
        // s now contains the string: '<div class="my-class">Some text</div>'
htmlDecode(JsString) Convert certain characters (&, <, >, ', and ") from their HTML character equivalents.
htmlEncode(JsString) Convert certain characters (&, <, >, ', and ") to their HTML character equivalents for literal display in web pages.
leftPad(JsString, JsNumber, object) Pads the left side of a string with a specified character. This is especially useful for normalizing number and date strings. Example usage:
var s = Ext.String.leftPad('123', 5, '0');
        // s now contains the string: '00123'
repeat(JsString, JsNumber, JsString) Returns a string with a specified number of repititions a given string pattern. The pattern be separated by a different string.
var s = Ext.String.repeat('---', 4); // = '------------'
           var t = Ext.String.repeat('--', 3, '/'); // = '--/--/--'
resetCharacterEntities() Resets the set of character entity definitions used by htmlEncode and htmlDecode back to the default state.
splitWords(object) Splits a string of space separated words into an array, trimming as needed. If the words are already an array, it is returned.
toggle(JsString, JsString, JsString) Utility function that allows you to easily switch a string between two alternating values. The passed value is compared to the current string, and if they are equal, the other value that was passed in is returned. If they are already different, the first value passed in is returned. Note that this method returns the new value but does not change the current string.
// alternate sort directions
    sort = Ext.String.toggle(sort, 'ASC', 'DESC');
    // instead of conditional logic:
    sort = (sort == 'ASC' ? 'DESC' : 'ASC');
trim(JsString) Trims whitespace from either end of a string, leaving spaces within the string intact. Example: @example
var s = '  foo bar  ';
            alert('-' + s + '-');         //alerts "- foo bar -"
            alert('-' + Ext.String.trim(s) + '-');  //alerts "-foo bar-"
uncapitalize(JsString) Uncapitalize the given string
urlAppend(JsString, JsString) Appends content to the query string of a URL, handling logic for whether to place a question mark or ampersand.
© Copyright 2005-2011 SharpKit. All rights reserved.