Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:String

From MDC

目录

[编辑] 概要

核心对象

An object representing a series of characters in a string.

[编辑] 如何创建

字符串构造:

new String(string)

[编辑] 参数

string 
一个字符串。

[编辑] 描述

通过new String()构造一个的字符串:

s_obj = new String("foo"); // 创建一个新的字符串对象

String 对象封装了Javascript的字符串类型,以及下面将要提到的方法。全局函数String()也可以用来创建原始的字符串 :

s_prim = String("bar"); //创建一个原始字符串

Literal strings in Javascript source code create primitive strings:

s_also_prim = "foo"; // creates a primitive string

因为 Javascript 可以自动转换原始字符串和字符串对象,你可以在原始字符串上调用字符串对象的任何方法,JavaScript会自动将原始字符串转换成一个临时的字符串对象,然会调用方法, 然后抛弃临时对象。例如:你可以在一个原始字符串上使用String.length 属性:

s_obj.length;       // 3
s_prim.length;      // 3
s_also_prim.length; // 3
'foo'.length;       // 3
"foo".length;       // 3

(A string literal can use single or double quotation marks.)

String objects can be converted to primitive strings with String.valueOf().

String primitives and String objects give different results when evaluated as Javascript. Primitives are treated as source code; String objects are treated as a character sequence object. For example:

s1 = "2 + 2" // creates a string primitive
s2 = new String("2 + 2") // creates a String object
eval(s1)     // returns the number 4
eval(s2)     // returns the string "2 + 2"
eval(s2.valueOf()); // returns the number 4

You can convert the value of any object into a string using the global String function.

[编辑] 访问字符串中的单个字符

有两个方法来访问一个字符串中的单个字符,第一种是charAt 方法:

'cat'.charAt(1) // returns "a"

第二种是将字符串看作是一个数组,通过索引来访问字符:

'cat'[1] // returns "a"

注意:第二种方式(将字符串看作为数组)不是 ECMAScript的 一部分;这仅仅是JavaScript的特性。

两者方式中,均不能设置单个字符的值。尝试通过charAt设置单个字符会返回一个错误:

var x = 'cat';
x.charAt(0) = 'b'; // error

通过数组索引方式来设置单个字符不会抛出错误,但是也不会改变原是字符串的值:

var x = 'cat';
x[2] = 'p';
alert(x);    // still outputs 'cat'
alert(x[2]); // still outputs 't'

[编辑] 字符串比较

C 语言中有strcmp() 函数用来比较字符串。在JavaScript立,你只需要使用比较操作符大于号与小于号来进行字符串比较:

var a = "a";
var b = "b";
if (a < b) // true
    document.write(a + " 小于 " + b);
else if (a > b)
    document.write(a + " 大于 " + b);
else
    document.write(a + " 和 " + b + " 相等。");

[编辑] Properties

  • constructor: Specifies the function that creates an object's prototype.
  • length:返回字符串的长度
  • prototype: Allows the addition of properties to a String object.

[编辑] 静态方法

  • fromCharCode: Returns a string created by using the specified sequence of Unicode values.

[编辑] Methods

[编辑] 与html标签无关的方法

  • charAt: 返回指定索引处的字符。
  • charCodeAt: 返回指定索引出的字符的Unicode值。
  • concat: 将两个字符串合并,并作为新字符串返回。
  • indexOf: 返回 String 对象内第一次出现子字符串的字符位置。没有找到则返回 -1。
  • lastIndexOf:返回 String 对象中子字符串最后出现的位置。
  • match: 用来用正则表达式对字符串进行匹配。
  • replace: 在字符串中搜索匹配的子字符串,并将其替换为一个新字符串。Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
  • search: Executes the search for a match between a regular expression and a specified string.
  • slice: Extracts a section of a string and returns a new string.
  • split: 将字符串分割为一个字符串数组。
  • substr: 返回从指定索引出开始的、指定字符数的字符串。
  • substring: 返回两个索引之间的子字符串。
  • toLowerCase: 返回字符串的小写形式。Returns the calling string value converted to lowercase.
  • toSource: Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the Object.toSource method.
  • toString: Returns a string representing the specified object. Overrides the Object.toString method.
  • toUpperCase: 返回字符串的大写形式
  • valueOf: Returns the primitive value of the specified object. Overrides the Object.valueOf method.

[编辑] HTML wrapper methods

每一个方法都会将字符串环绕了某一html标签后返回。例如:"test".bold() 返回 "<b>test</b>".

这些方法的使用度有限,因为仅能提供部分html标签,和属性。

[编辑] Examples

[编辑] 例子:原始字符串

下面的语句直接将从字符串中创建原始符串并赋值给last_name变量:

var last_name = "Schaefer";

[编辑] Example: String primitive properties

The following expressions evaluate to 8, "SCHAEFER", and "schaefer", respectively:

last_name.length
last_name.toUpperCase()
last_name.toLowerCase()

[编辑] Example: Setting an individual character in a string

An individual character cannot be directly set in a string. Instead, a new string can be created using the substr or substring methods:

function setCharAt(str, index, ch) {
   return str.substr(0, index) + ch + str.substr(index + 1);
}

alert(setCharAt('scam', 1, 'p')); // outputs "spam"

[编辑] Example: Pass a string among scripts in different windows or frames

The following code creates two string variables and opens a second window:

var lastName = "Schaefer";
var firstName = "Jesse";
empWindow = window.open('string2.html', 'window1', 'width=300,height=300');

If the HTML source for the second window (string2.html) creates two string variables, empLastName and empFirstName, the following code in the first window assigns values to the second window's variables:

empWindow.empFirstName = firstName;
empWindow.empLastName = lastName;

The following code in the first window displays the values of the second window's variables:

alert('empFirstName in empWindow is ' + empWindow.empFirstName);
alert('empLastName in empWindow is ' + empWindow.empLastName);