Stringsstriŋ字符串

A string is a sequence of Unicode letters, digits, punctuation characters, and so on; it is the JavaScript datatype for representing text.
字符串是由Unicode字符、数字、标点符号等组成的序列,它是JavaScript用来表示文本的数据类型。

To represent a single character, you simply use a string that has a length of 1.
要表示单个字符,必须使用长度为1的字符串。

String Literalsˈlitərəl字符串直接量

You can include string literals in your programs by enclosing them in matching pairs of single or double quotation marks.
程序中的字符串直接量是包含在单引号或双引号中的。

The ECMAScript v1 standard allows Unicode characters within string literals.
ECMAScript v1标准 允许字符串直接量使用Unicode字符。

Escape Sequences in String Literals 字符串直接量中的转义序列

Since the apostrophe is the same as the single-quote character, you must use the backslash character () to escape any apostrophes that appear in single-quoted strings.
由于撇号与单引号相同,必须使用反斜线符号 () 来转义带有单引号的字符串中出现的撇号。

序列 Character represented 所代表的字符
\0 The NUL character (\u0000) NUL字符 (\u0000)
\b Backspace (\u0008) 退格符 (\u0008)
\t Horizontal tab (\u0009) 水平制表符 (\u0009)
\n Newline (\u000A) 换行符 (\u000A)
\v Vertical tab (\u000B) 垂直制表符 (\u000B)
\f Form feed (\u000C) 换页符 (\u000C)
\r Carriage return (\u000D) 回车符 (\u000D)
\” Double quote (\u0022) 双引号 (\u0022)
\’ Apostrophe or single quote (\u0027) 撇号或单引号 (\u0027)
\\ Backslash (\u005C) 反斜线符 (\u005C)
\xXX The latin-1 character specified by the two hexadecimal digits XX 由2位16进制数值XX指定的Latin-1字符
\uXXXX The Unicode character specified by the four hexadecimal digits XXXX 由4位16进制数值XXXX指定的Unicode字符

Note that the backslash escape cannot be used before a line break to continue a string (or other JavaScript) token across two lines or to include a literal line break in a string.
注意,不能在换行符前用反斜线转义符使字符串(或其他JavaScript)标记跨两行或在字符串中包含一个换行直接量。

Working with Strings 字符串的使用

One of the built-in features of JavaScript is the ability to concatenate strings.
JavaScript的内部特性之一就是能够连接字符串。

If you use the + operator on strings, it joins them by appending the second to the first.
如果将加号(+)运算符用于字符串,它就会把这两个字符串连接起来,将第二个字符串附加在第一个之后。

For example:

msg = “崔凯” + “的博客”;
//崔凯的博客

To determine the length of a string (the number of characters it contains) use the length property of the string.
要确定一个字符串的长度(它包含的字符数),可以使用字符串的length属性。

If the variable s contains a string, you access its length like this:
如果变量s包含一个字符串,可以使用如下的方式访问它的长度:
s.length

You can use a number of methods to operate on strings. For example, to get the last character of a string s:
可以使用针对字符串的许多操作。例如,可以获取字符串s的最后一个字符:
last_char = s.charAt(s.length - 1)

To extract the second, third, and fourth characters from a string s:
可以从字符串s中抽出第二、三、四个字符:
(注解:substring() 方法返回的子串包括 start 处的字符,但不包括 end 处的字符。)
sub = s.substring(1,4);

To find the position of the first letter “a” in a string s:
要在字符串s中查找第一个字母“a”的位置:
i = s.indexOf('a');

JavaScript strings and JavaScript arrays, are indexed starting with zero.
JavaScript的字符串和JavaScript数组一样,都是以0开始索引的。

Converting Numbers to Strings 把数字转换为字符串

Numbers are automatically converted to strings when needed.
数字会在需要的时候自动转换为字符串。

If a number is used in a string concatenation expression, for example, the number is converted to a string first:
例如,如果一个数字用在一个字符串连接表达式中,数字就会先转换为字符串:

var n = 2;
var s = n + “只老虎”
//2只老虎

To make number-to-string conversions more explicit, use the String() function:
要让数字更清楚的转换为字符串,可以使用String()函数:
var string_value = String(number);

Another technique for converting numbers to strings uses the toString() method:
把数字转换为字符串的另一种方法是使用toString()方法:
string_value = number.toString();

toFixed() converts a number to a string and displays a specified number of digits after the decimal point. It does not use exponential notation.
toFixed() 方法把一个数字转换为字符串,并且显示小数点后指定的位数。它不使用指数表示法。

toExponential() converts a number to a string using exponential notation, with one digit before the decimal point and a specified number of digits after the decimal point.
toExponential() 使用指数表示法把一个数字转换为字符串,小数点前面有1位数,而小数点后面有指定的位数。

toPrecision() displays a number using the specified number of significant digits.
toPrecision() 使用指定的有意义位数来显示一个数字。

Note that all three methods round the trailing digits of the resulting string as appropriate.
注意,这3个方法都会对结果字符串进行适当的四舍五入。


var n = 123456.789;
n.toFixed(0); //1234567
n.toFixed(2); //123456.79 四舍五入
n.toExponential(1); //1.2e+5
n.toExponential(3); //1.235e+5
n.toPrecision(4); //1.235e+5
n.toPrecision(7); //123456.8

Converting Strings to Numbers 把字符串转换为数字

When a string is used in a numeric context, it is automatically converted to a number.
当一个字符串用于数字环境中,它也会自动地转换为一个数字。

var product = “21” * “2”; //结果为数字42

A less tricky and more explicit way to convert a string to a number is to call the Number() constructor as a function:
将一个字符串转换为数字更清楚明白的方法就是把Number()构造函数作为一个函数来调用:
var number = Number(string_value);

The trouble with this sort of string-to-number conversion is that it is overly strict.
这种把字符串转换为数字的方法,麻烦之处在于它过于严格。

It works only with base-10 numbers, and although it allows leading and trailing spaces, it does not allow any nonspace characters to appear in the string following the number.
它只对以10为基数的数字有效,并且尽管它允许开头和结尾的空白,但是,在紧随数字的字符串中,它不允许出现任何非空字符。

To allow more flexible conversions, you can use parseInt() and parseFloat().
要允许更多灵活的转化,可以使用parseInt() 和 parseFloat()

These functions convert and return any number at the beginning of a string, ignoring any trailing nonnumbers.
这些函数可以从字符串开始处转换和返回任何数字,忽略结尾的非数字部分。

parseInt() parses only integers, while parseFloat() parses both integers and floating-point numbers.
parseInt() 只截取整数,而parseFloat() 截取整数和浮点数。


parseInt(“3 idiots”); //返回 3
parseFloat(“3.14 meters”); //返回 3.14
parseInt(“12.34”); //返回 12

If parseInt() or parseFloat() cannot convert the specified string to a number, it returns NaN.
如果 parseInt() 和 parseFloat() 不能把指定的字符串转换为数字,它们就会返回NaN

Referenceˈrefrəns参考

翻译 ← 可将本链接拖动到“书签栏”,选中生词后,点击翻译。

《JavaScript权威指南》