1.String(字符串)对象
length 属性返回字符串的长度
var txt = "ABCDEF";
var sln = txt.length;
ndexOf( )方法返回字符串中指定文本*首次*出现的索引(位置):0 是字符串中的第一个位置,1 是第二个,2 是第三个 ...
var str = "The full name of China is the People's Republic of China";
var pos = str.indexOf("China");
lastIndexOf( )方法返回指定文本在字符串中*最后*一次出现的索引
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");
两种方法都接受作为检索起始位置的第二个参数
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18);
var pos = str.lastIndexOf("China", 50);
search()方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");
有三种提取部分字符串的方法:
- slice(start, end)
- substring(start, end)
- substr(start, length)