Udostępnij za pośrednictwem


String Object

Allows manipulation and formatting of text strings and determines and locates substrings within strings.

function String([stringLiteral : String])

Arguments

  • stringLiteral
    Optional. Any group of Unicode characters.

Remarks

String objects can be created implicitly using string literals. String objects created in this fashion (referred to as "primitive" strings) are treated differently from String objects created using the new operator. Although you can read properties and call methods on primitive strings, you cannot create new properties or add methods to them.

Escape sequences can be used in string literals to represent special characters that cannot be used directly in a string, such as the newline character or Unicode characters. At the time a script is compiled, each escape sequence in a string literal is converted into the characters it represents. For additional information, see String Data.

JScript also defines a String data type, which provides different properties and methods from the String object. You cannot create properties or add methods to variables of the String data type, while you can for instances of the String object.

The String object interoperates with String data type (which is the same as the System.String data type). This means that a String object can call the methods and properties of the String data type, and a String data type can call the methods and properties of the String object. For more information, see String. Furthermore, String objects are accepted by functions that take String data types, and vice versa.

The data type of a String object is Object, not String.

Example 1

This script demonstrates that although the length property can be read and the toUpperCase method can be called, the custom property myProperty cannot be set on the primitive string:

var primStr : Object = "This is a string";
print(primStr.length);           // Read the length property.
print(primStr.toUpperCase());    // Call a method.
primStr.myProperty = 42;         // Set a new property.
print(primStr.myProperty);       // Try to read it back.

The output of this script is:

16
THIS IS A STRING
undefined

Example 2

For String objects created with the new statement, custom properties can be set:

var newStr : Object = new String("This is also a string");
print(newStr.length);           // Read the length property.
print(newStr.toUpperCase());    // Call a method.
newStr.myProperty = 42;         // Set a new property.
print(newStr.myProperty);       // Try to read it back.

The output of this script is:

21
THIS IS ALSO A STRING
42

Properties and Methods

String Object Properties and Methods

Requirements

Version 1

See Also

Reference

Object Object

String Data Type (Visual Studio - JScript)

new Operator

Concepts

String Data