|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: RegExp] [Home] [Next: Color]

#### String

A `String` is a sequence of zero or more Unicode characters. Qt Script's `String` class uses the Qt QString class's functions and syntax.

Strings can be created and concatenated as follows.

        var text = "this is a";
        var another = new String( "text" );
        var concat = text + " " + another;  // concat == "this is a text"

##### String Properties

- length : Number; The length property specifies the length of the string.

##### Static String Functions

- fromCharCode ( static function )

          var s = String.fromCharCode( 65, 66, 67, 68 );
          System.println( s ); // prints "ABCD"

  Returns a string made up of the characters with code `code1`, `code2`, etc, according to their Unicode character codes.

##### String Functions

- charAt( pos : Number ) : String; Returns the character in the string at position `pos`. If the position is out of bounds, `undefined` is returned.

- charCodeAt( pos : Number ) : Number; Returns the character code of the character at position `pos` in the string. If the position is out of bounds, `undefined` is returned.

- indexOf( pattern : String or RegExp, pos : Number ) : Number; Returns the index of `pattern` in the string, starting at position `pos`. If no position is specified, the function starts at the beginning of the string. If the pattern is not found in the string, -1 is returned.

- lastIndexOf( pattern : String or RegExp, pos : Number ) : Number; Returns the last index of `pattern` in the string, starting at position `pos` and searching backwards from there. If no position is specified, the function starts at the end of the string. If the pattern is not found in the string, -1 is returned.

- match( pattern : RegExp ) : String; Returns the matched pattern if this string matches the pattern defined by `regexp`. If `regexp` is not a valid regular expression, `undefined` is returned.

- search( pattern : String or RegExp ) : String; same as find.

- searchRev( pattern : String or RegExp ) : String; same as findRev.

- replace( pattern : RegExp, newValue : String ) : String; Replaces the first occurrence of `pattern` in the string with `newvalue` if the pattern is found in the string. A modified copy of string is returned.

  If `pattern` is a regular expression with global set, all occurances of `pattern` in the string will be replaced.

- split( pattern : String or RegExp ) : String[]; Returns an array of strings containing this string split on each occurrence of `pattern`.

- substring( startIndex : Number, endIndex : Number ) : String; Same as substr.

- toLowerCase() : String; Returns a lowercase copy of this string.

- lower() : String; Same as toLowerCase().

- toUpperCase() : String; Returns an uppercase copy of this string.

- upper() : String; Same as toUpperCase().

- isEmpty() : Boolean; Returns true if the string is empty, i.e. has a length of 0; otherwise returns false.

- left( length : Number ) : String; Returns a substring containing the `length` leftmost characters of this string.

- right( length : Number ) : String; Returns a substring containing the `length` rightmost characters of this string.

- mid( start : Number, length : Number ) : String; Same as substring above.

- find( pattern : String or RegExp, pos : Number ) : Number; Returns the first position of `pattern` after `pos`. If the pattern is not found, -1 is returned. If `pos` is not specified, position 0 is used.

- findRev( pattern : Number, pos : Number ) : String; Returns the first position of `pattern` before `pos`, searching backward. If pattern is not found, -1 is returned. If `pos` is not specified, the search starts at the end of the string.

- startsWith( pattern : String or RegExp ) : Boolean; Returns true if the string starts with `pattern`; otherwise returns false.

- endsWith( pattern : String or RegExp ) : Boolean; Returns true if the string ends with `pattern`; otherwise returns false.

- arg( value : String or Number, fieldWidth : Number ) : String; This function will return a string that replaces the lowest numbered occurrence of %1, %2, ..., %9 with `value`.

  The `fieldWidth` parameter specifies the minimum amount of space that `value` is padded to. A positive `fieldWidth` will produce right-aligned text, whereas a negative `fieldWidth` will produce left-aligned text.

- argInt( value : Number, fieldWidth : Number, base : Number ) : String; This function behaves like arg above, but is specialized for the case where `value` is an integer.

  `value` is expressed in base `base`, which is 10 by default and must be between 2 and 36.

- argDec( value : Number, fieldWidth : Number, format : Number, precision : Number ) : String; This function behaves like `arg()` above, but is specialized for the case where `value` is a decimal value.

  Argument `value` is formatted according to the `format` specified, which is 'g' by default and can be any of the following:

  - `e` - format as [-]9.9e[+|-]999

  - `E` - format as [-]9.9E[+|-]999

  - `f` - format as [-]9.9

  - `g` - use `e` or `f` format, whichever is the most concise

  - `G` - use `E` or `f` format, whichever is the most concise

  With 'e', 'E', and 'f', `precision` is the number of digits after the decimal point. With 'g' and 'G', `precision` is the maximum number of significant digits (trailing zeroes are omitted).

[Prev: RegExp] [Home] [Next: Color]