String 对象 JavaScript

2025-10-29 18:58:51

1、语法

newString = new String(["stringLiteral"])

String 对象 JavaScript

2、参数

newString

必需。   String 对象分配到的变量名称。  

stringLiteral

可选。  任何 Unicode 字符组。  

String 对象 JavaScript

3、字符串

字符串文本是以单引号或双引号括起零个或多个字符。  字符串文本具有 string 的主(基元)数据类型。   String 对象通过使用 new 运算符创建,其数据类型为 Object。

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

var strObj = new String("This is a string object.");

document.write(typeof strLit);

document.write("<br/>");

document.write(typeof strObj);

  

String 对象 JavaScript

4、字符串文本上调用方法

在字符串文本上调用方法时,该方法将临时转换为字符串包装器对象。  字符串文本将被视为好像是使用 new 运算符创建的。

var strLit = "This is a string literal.";var result1 = strLit.toUpperCase();

var result2 = (new String(strLit)).toUpperCase();

document.write(result1);document.write("<br/>"); 

document.write(result2);

String 对象 JavaScript

5、访问单个字符

可以将字符串的单个字符作为只读数组索引属性进行访问。  Internet Explorer 9 标准模式、Internet Explorer 10 标准模式、Internet Explorer 11 标准模式和 Windows 应用商店应用 中已引入此功能。  以下示例访问单个字符串字符。

var str = "abcd";var result = str[2]; 

document.write (result);// Output: c

var result = "the"[0]; 

document.write(result);

String 对象 JavaScript

6、属性

constructor 属性  指定用于创建对象的函数

length 属性(字符串)  返回 String 对象的长度。

prototype 属性   为对象的类返回原型的引用。

String 对象 JavaScript

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢