Partager via


String Object (Windows Scripting - JScript)

 

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

Syntax

newString = new String(["stringLiteral"])

Arguments

  • newString
    Required. The variable name to which the String object is assigned.

  • stringLiteral
    Optional. Any group of Unicode characters.

Remarks

JScript provides escape sequences that you can include in strings to create characters that you cannot type directly. For example, \t specifies a tab character. For more information, see Data Types (Windows Scripting - JScript).

Data Types

A string literal is zero or more characters enclosed in single or double quotation marks. A string literal has a primary (primitive) data type of string. A String object is created by using the new Operator, and has a data type of object.

The following example defines a string literal and a string object and determines their data types.

// Define a string literal and a string object.
var strLit = "This is a string literal.";
var strObj = new String("This is a string object.");

// Display the types of the string literal and object.
alert(typeof strLit);
// Output: string

alert(typeof strObj);
// Output: object

Methods for String Literals

When JScript accesses a method of a primitive string, the string is temporarily converted to a string wrapper object. JScript accesses the method from the wrapper object. The primitive string is treated as if the new operator were used to create it.

The following example applies the toUpperCase method to a primitive string.

var strLit = "This is a string literal.";

// Return a string converted to uppercase.
// The strLit primitive string is converted to a temporary
// wrapper object that accesses the toUpperCase method.
var result = strLit.toUpperCase();

// This statement is equivalent to the above statement.
var result = (new String(strLit)).toUpperCase();
// Output: THIS IS A STRING LITERAL.

Adding Properties Dynamically

A String object declared with the new operator can have its own set of dynamically created properties. In the following example, the test property defined for the gamma object is not defined for delta.

var gamma = new String("This is a string");
var delta = new String("This is also a string");

gamma.test = 10;

Properties

constructor Property | length Property | prototype Property

Methods

anchor Method | big Method | blink Method | bold Method | charAt Method | charCodeAt Method | concat Method | fixed Method | fontcolor Method | fontsize Method | fromCharCode Method | indexOf Method | italics Method | lastIndexOf Method | link Method | localeCompare Method | match Method | replace Method | search Method | slice Method | small Method | split Method | strike Method | sub Method | substr Method | substring Method | sup Method | toJSON Method | toLocaleLowerCase Method | toLocaleUpperCase Method | toLowerCase Method | toUpperCase Method | toString Method | valueOf Method

Requirements

Version 1

Change History

Date

History

Reason

September 2010

Added information about wrapper objects.

Information enhancement.

September 2010

Modified information about dynamically created properties.

Customer feedback.

August 2008

Added link to toJSON.

Information enhancement.

See Also

new Operator (Windows Scripting - JScript)