String object

String object

Properties

length
integer, number of characters in the string. Read-only property.
[index]
integer, code of character at the index position, Read-write index accessor. Zero-based index.
[begin..end]
integers, zero-based indexes of first and last character. Returns string slice contained characters from start index and up to but not included end index. Begin or/and end are optional. If begin is ommited then it is assumed to be 0, if end – length is used as end value.

Iteration

for (var codepoint in str) {...}

Iterates all UNICODE code points in string. Internally strings are stored as arrays of UTF16 code units and str.length reports number of UTF16 code units. In contrary, on each iteration the codepoint variable receives integer value – UNICODE code point. Note that number of iterations can be less than str.length if the string contains so called surrogate pairs – symbols presented by two UTF16 code units.

for (var (strpos,codepoint) in str) {...}

Does the same as the above but also reports position (index) of the codepoint in string.

Methods

toInteger

([defaultValue[, radix:integer]]) : integer | defaultValue | (undefined)

Tries to parse content of the string. If parsing failed then returns defaultValue if provided, or undefined value.
toInteger expects string in the following format:

[whitespace] [{+ | -}] [0 [{ x | X }]] [digits]
toFloat

([defaultValue]) : В float | defaultValue | (undefined)

Tries to parse content of the string. If parsing failed then returns defaultValue if provided, or undefined value.
toInteger expects string in the following format:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits]
toNumber

([defaultValue]) : integer | float | defaultValue | (undefined)

Tries to parse the string into either float or integer value. This is an equivalent of:

var n = s.toInteger( s.toFloat() );
toString

() : string

Returns string itself.

toHtmlString

() : string

Returns string escaped by html rules. Is an alias of the htmlEscape() method.

toUrlString

() : string

Returns string escaped by url rules. Is an alias of the urlEscape() method.

substring

(start [,end]) : string | undefined

start and end are integers – zero-based indexes of first and last character. Method returns string slice consisting from characters starting from start index and up to but not included end index. If end is ommited it is interpretted as equal to length.

Negative values of start or end treated as a "right side indexes" thus expression "Script".substring(0,-1) == "Script" is valid.

substr
(start [,length]) : string | undefined

start and length are integers. Start is zero-based index of first character and length is a number of characters in the slice.

Negative value of start interpreted as a "right side index" thus expression "Script".substr(-6) == "Script" is valid.

slice

(start [,end]) : string | undefined

Equivalent of substring method.

splice
(index: int, count: int [, string1, string2, …] ) returns: string

This method:

  1. Removes count number of characters at index.
  2. Inserts string1, string2, … at index position.
  3. Returns new string – result of the operation.
concat

( [string1[, string2[, … [, stringN]]] ) : string

Returns string consisting from concatenated arguments: self + string1 + string2 + string3 + … + stringN.

charAt

( index ) : string.

Returns one character string. Equivalent of substr( index, 1 ). If index is out of bounds of the string then charAt returns empty string.

charCodeAt

( index ) : integer | undefined

Returns (uni)code of character at index position.

indexOf

( substring [,start] ) : integer

Searches this string for text in substring. Returns index of first occurence of substring or -1 if not found.

lastIndexOf
( substring [,start] ) : integer

Searches this string for text in substring. Returns index of last occurence of substring or -1 if not found.

lexicalCompare

( what , [ caseInsensitive: true | false ] ) : integer

Compares this string with what string using lexicographic character order. For example: "ABC" < "abc" В is true but "ABC".lexicalCompare("abc") yelds to >= 1. If caseInsensitive is provided and true it does case insensitive comparison. By default comparison is case sensitive.

match

( regexp ) : string | array of strings | null value

Returns fragment(s) of the string which satisfy regexp.
match

( string ) ‘ RegExp object instance | null value

Returns RegExp object instance which satisfy string pattern.
replace

( regexp, replaceBy: string | function ) : string

Returns copy of the string where all fragments satisfying regexp are replaced by replaceBy. If replaceBy is a function then this function will be called for each matching substring with parameters corresponding to the whole match and each matched sub-group.

This example:

var s = "212F";
var test = /(\d+(?:\.\d*)?)F\b/g;
function f2c(str, g1)
{
   return ((g1.toNumber() - 32) * 5/9) + "C";
}
stdout.printf("%V\n", s.replace(test, f2c ));

will convert each Fahrenheit to Celsius number in the given string (s).

search

( regexp ) : integer

Returns index of first occurence of string fragment satisfying regexp or -1 if not found.

split

(separator[, limit]) : string

Splits the string separated on components by separator. Separator is either regexp object or string. Returns array of strings – substrings between separators. Limit is an integer – maximum number of elements in returned array.

fromCharCode

([code1[, code2[, [, codeN]]]]) : string

Static method. Returns string build from character with given integer codes.

toLowerCase

() : string

Returns lower case copy of the string.

toUpperCase
() : string

Returns upper case copy of the string.

trim
( [#left | #right] ) : string

Returns copy of the string with spaces removed.

urlEscape

() : string

Returns url-escaped copy of the string if it contains characters need to be escaped or string itself if there are no such characters. Note that non-ascii characters are converted to utf-8 sequences first and resulting codes will be escaped.

urlUnescape
() : string

Restores url-escaped string.

htmlEscape

() : string

Returns string where each < > & " or В character replaced by &lt; &gt; &amp; &quot; or &apos; sequence.

htmlUnescape
() : string

Returns string where html entities replaced by correspondent character codes.

$

() : string

static method, returns composed string. Note, this is a "stringizer" method.

Example, this:

var pt = { x:12, y:42  };
var msg = String.$(coordinates are x:{ pt.x } y:{ pt.y });

produces string "coordinates are x:12 y:42" В in msg variable.

printf

( format, [value1[, value2[, [, valueN]]]]) : string

Static method. Returns string formatted by the rules of sprintf C/C++ function.

Additional format types:

    %v and %V – these format types accept any value as an argument and produce source code representation of the value suitable for later parsing by eval() method. Thus if value is an array of values it will be printed as "[element1, element2, element3… elementN]" and object (instance of Object class) will be printed as "{key1:value1, key2:value2,…, keyN:valueN}". %v produces one line output and %V tries to produce human readable output with line feeds and tabulations.
    Use it if you need to serialize objects in AJAX/JSON fashion.

    %S – this format type converts its argument into string and outputs it with HTML escapement. So characters like ‘<‘ will be converted to "&lt;" sequences in the output.

scanf
( format: string ) : array

Scans the string for values according to the rules of scanf C/C++ function with wildcard (like %[a-z] ) extensions. See definition of scanf in MSDN.

Additional format types:

    %N – scans for either float or integer number.

Function returns array of successfully parsed values.