JavaScript - The Strings Object




JavaScript - The Strings Object

A String is a sequence of symbols or digits. Strings are among the most frequently used data types. Like any other data types, strings in TestComplete are represented as OLE-compatible variants. In JavaScript, a sequence of literal characters enclosed in a matching pair of quotation marks is recognized as a string. The quotation marks could be either single (') or double ("). The quotation characters are allowed within a string if they do not match the enclosing pair. All the following strings are valid −

str1 = "The brig was heading to Liverpool, when the captain noticed a ship.";
str2 = 'It came out of night fog and looked weird: no sails, no lights and no crew.';
str3 = "'Ahoy! Is there anyone?' - the captain cried.";
str4 = '"Nobody." - was the answer.'

To deal with strings, TestComplete has a special aqString scripting object. The object is available for all supported scripting languages, so that you can use it to operate with string values regardless of the chosen language.

Syntax

Use the following syntax to create a String object −

var val = new String(string);

The String parameter is a series of characters that has been properly encoded.

What is an JavaScript The Strings Object ?

A string is a set of characters, including letters, numbers, spaces, etc. In JavaScript, a String is a global object, a constructor for strings or a sequence of characters. It stores all the textual data in JavaScript; there is no separate datatype for a single character.

Syntax of JavaScript String

The syntax of defining a String literal is as follows -

var stringName = "string ";
var stringName = "string";

The syntax to create a String global object looks like this, where data is any object that is convertible to a string -

var stringName = new String(data);

With the introduction of ECMAScript 2015, string literals can be “Template literals”, enclosed by the back-ticks (` `). These allow us to embed an expression into a string by wrapping it in ${…} (jQuery syntax). These are also helpful when you want the string to span multiple lines. Its syntax appears as follows -

`string [${some expression}]`

Do you know how to create different objects? If not, then you must definitely go through our tutorial on JavaScript Objects.

The String Object

One way to create a String object is to use the new keyword. The syntax is shown here -

var instance_name = new String("string value here");

You replace instance_name with the name you want to use for the instance of the String object. You then replace string value here with the string of characters to use as the new String object. So, if you want to create an instance of the String object named guitar_string, you could use the following code -

var guitar_string = new String("G");

This script creates an instance of the String object for the string “G”. Although creating a String object with the new keyword can be useful for things such as comparing String objects, string literals are used more often.

The table below lists the JavaScript string methods, gives a brief description of each, and provides links to the pages where they are discussed and demonstrated -

For Example
Name Description
indexOf Search string; return location of match or -1.
lastIndexOf Search string; return location of last match or -1.
search Search string using regular expression; return location of match or -1.
match Search string using regular expression; return array containing results.
replace Return new string resulting from regular expression search and replace.
slice Return substring as specified by start and end locations in string.
substring Similar to slice, but doesn't support negative integer arguments.
substr Return substring as specified by start location and length.
split Return array of substrings by splitting string on separator you specify.
toUpperCase Return new string with all characters in uppercase.
toLowerCase Return new string with all characters in lowercase.
fromCharCode Static method; return string from list of Unicode numeric encodings.
trim Return new string with whitespace removed from beginning and end. (ES5)