A string is a sequence of Unicode letters, digits, punctuation characters, and so on; it is the JavaScript datatype for representing text.

To represent a single character, you simply use a string that has a length of 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.

Working with Strings 字符串的使用

One of the built-in features of JavaScript is the ability to concatenate strings.

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.

If the variable s contains a string, you access its length like this:

You can use a number of methods to operate on strings. For example, to get the last character of a string s:
last_char = s.charAt(s.length - 1)

To extract the second, third, and fourth characters from a string s:
(注解:substring() 方法返回的子串包括 start 处的字符,但不包括 end 处的字符。)
sub = s.substring(1,4);

To find the position of the first letter “a” in a string s:
i = s.indexOf('a');

JavaScript strings and JavaScript arrays, are indexed starting with zero.

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 + “只老虎”

To make number-to-string conversions more explicit, use the String() function:
var string_value = String(number);

Another technique for converting numbers to strings uses the toString() method:
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.

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:
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.

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


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