网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文

javascript字符串对象常用api函数小结

栏目: 网页设计 / 发布于: / 人气:2.7W

1. concat(str1,str2,···)

javascript字符串对象常用api函数小结

连接字符串

2. indexOf(str,start)

返回 str 在字符串中首次出现的位置

var str = "hello world";xOf("hello"); // xOf("o",5); // xOf("World"); // -1

3. lastIndexOf(str,start)

返回 str 在字符串中最后出现的位置

var str = "hello world";IndexOf("hello"); // IndexOf("o",3); // IndexOf("o",5); // 4

4. replace(regexp/substr,replacement)

在字符串中用一些字符替换另一些字符,或替换一个与正则匹配的`字串

var str = "I is Allen.";ace("is","am"); // "I am Allen."

5. slice(start,end)

返回字符串的片段

var str = "I am Jack.";e(3,7); // "m Ja"e(3); // "m Jack."e(3,-3); // "m Ja"

6. split(separator,limit)

将一个字符串分割为子串,然后将结果作为字符串数组返回

var str = "hello world";t(" "); // ["hello","world"]t(" ",1); // ["hello"]

7. substr(start,lenght)

返回一个从指定位置开始的指定长度的字串

var str = "how do you do";tr(4,2); // "do"tr(4); // "do you do"tr(4,0); // " "tr(4,-1); // " "tr(-3); // "do"

8. substring(start,end)

返回位于 string 对象中指定位置的字串,包含 start 处字符,但不包含 end 处字符

var str = "how do you do";tring(0,3); // "how"

9. toLowerCase()

把字符串转换为小写

10. toUpperCase()

把字符串转换为大写

var str = "How do you do";werCase(); // "how do you do"perCase(); // "HOW DO YOU DO"