JavaScript 字符串方法
- 字符串长度 :length 属性返回字符串的长度
- 查找字符串中的字符串 :
indexOf()
方法返回字符串中指定文本首次出现的索引(位置):
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
0 是字符串中的第一个位置,1 是第二个,2 是第三个 :
lastIndexOf()
方法返回指定文本在字符串中最后一次出现的索引:
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");
如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
- 检索字符串中的字符串:
search()
方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");
两种方法,indexOf() 与 search(),是相等的。
这两种方法是不相等的。区别在于:
search() 方法无法设置第二个开始位置参数。
indexOf() 方法无法设置更强大的搜索值(正则表达式)。
- 提取部分字符串
- 有三种提取部分字符串的方法:
slice(start, end)
substring(start, end)
substr(start, length)
slice()
方法:
slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。
该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。
这个例子裁剪字符串中位置 7 到位置 13 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(7,13);
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);
res 的结果是
Banana
substring() 方法
substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。
var str = "Apple, Banana, Mango";
var res = str.substring(7,13);
substr() 方法
substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。
var str = "Apple, Banana, Mango";
var res = str.substr(7,6);
如果首个参数为负,则从字符串的结尾计算位置。
var str = "Apple, Banana, Mango";
var res = str.substr(-5);
第二个参数不能为负,因为它定义的是长度。
替换字符串内容:replace() 方法不会改变调用它的字符串。它返回的是新字符串。
默认地,replace() 只替换首个匹配:
str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "W3School");
默认地,replace() 对大小写敏感。因此不对匹配 MICROSOFT:
str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", "W3School");
如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感):
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");
转换为大写和小写:通过 toUpperCase() 把字符串转换为大写:
var text1 = "Hello World!"; // 字符串
var text2 = text1.toUpperCase(); // text2 是被转换为大写的 text1
通过 toLowerCase() 把字符串转换为小写:
var text1 = "Hello World!"; // 字符串
var text2 = text1.toLowerCase(); // text2 是被转换为小写的 text1
concat() 方法::连接两个或多个字符串:
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);
concat() 方法可用于代替加运算符。下面两行是等效的:
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ","World!");
所有字符串方法都会返回新字符串。它们不会修改原始字符串。
正式地说:字符串是不可变的:字符串不能更改,只能替换。
String.trim():trim() 方法删除字符串两端的空白符:
var str = " Hello World! ";
alert(str.trim());
提取字符串字符这是两个提取字符串字符的安全方法:
charAt(position)
charCodeAt(position)
charAt() 方法
charAt() 方法返回字符串中指定下标(位置)的字符串:
var str = "HELLO WORLD";
str.charAt(0); // 返回 H
charCodeAt() 方法
charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码:
var str = "HELLO WORLD";
str.charCodeAt(0); // 返回 72
把字符串转换为数组:可以通过 split() 将字符串转换为数组:
var txt = "a,b,c,d,e"; // 字符串
txt.split(","); // 用逗号分隔
txt.split(" "); // 用空格分隔
txt.split("|"); // 用竖线分隔
JavaScript 数值方法
- String() 方法:
toString()
以字符串返回数值,所有数字方法可用于任意类型的数字(字面量、变量或表达式):
var x = 123;
x.toString(); // 从变量 x 返回 123
(123).toString(); // 从文本 123 返回 123
(100 + 23).toString(); // 从表达式 100 + 23 返回 123
toExponential()
方法:toExponential() 返回字符串值,它包含已被四舍五入并使用指数计数法的数字,参数定义小数点后的字符数:
var x = 9.656;
x.toExponential(2); // 返回 9.66e+0
x.toExponential(4); // 返回 9.6560e+0
x.toExponential(6); // 返回 9.656000e+0
toFixed()
方法:toFixed() 返回字符串值,它包含了指定位数小数的数字:
var x = 9.656;
x.toFixed(0); // 返回 10
x.toFixed(2); // 返回 9.66
x.toFixed(4); // 返回 9.6560
x.toFixed(6); // 返回 9.656000
toPrecision()
方法:toPrecision() 返回字符串值,它包含了指定长度的数字
var x = 9.656;
x.toPrecision(); // 返回 9.656
x.toPrecision(2); // 返回 9.7
x.toPrecision(4); // 返回 9.656
x.toPrecision(6); // 返回 9.65600
valueOf()
方法:valueOf() 以数值返回数值:
var x = 123;
x.valueOf(); // 从变量 x 返回 123
(123).valueOf(); // 从文本 123 返回 123
(100 + 23).valueOf(); // 从表达式 100 + 23 返回 123
- 把变量转换为数值,这三种 JavaScript 方法可用于将变量转换为数字:
Number() 方法
parseInt() 方法
parseFloat() 方法