String object

Properties

length
- integer, number of characters in the string. Read-only property.
[index]
- integer, code of character at the index position, Read-only index accessor. Zero-based index. Note that the index accesses string as a vector of UTF16 code units - characters out of BMP may take two positions.
[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 omitted 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

new String
(len:integer [, initCharCode = ' '])

Creates new string of given size and fill. 

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.
toFloat 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
([ #as-parameter ]) : string

Returns string escaped by URL rules. It is an equivalent of URL.escape(string) or, if #as-parameter provided, URL.escapeParam(string).

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 omitted 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.

distance
( other: string ) : integer

The function returns Levenshtein distance between this string and the other. Distance is a non-negative integer, larger - less similarity between these strings.

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:
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:

Function returns array of successfully parsed values.

UID
( ) : string

Static method. Returns UUID - universally unique identifier. On Windows it uses UuidCreate() and libuuid on other platforms.