
1.计算字符串的长度
代码如下:
var txt="Hello World!"
document.write(txt.length)
2.indexOf() 方法
如何使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置。
代码如下:
var str="Hello world!" //w小写
document.write(str.indexOf("H") + "
") //0
document.write(str.indexOf("World") + "
") //-1
document.write(str.indexOf("world")) //6
script>
3.match() 方法
查找字符串中特定的字符,并且如果找到的话,则返回这个字符。
代码如下:
var str="Hello world!"
document.write(str.match("world") + "
") //world
document.write(str.match("World") + "
") //null
document.write(str.match("worlld") + "
") //null
document.write(str.match("world!")) //world!
script>
4.如何替换字符串中的字符 - replace()
用 replace() 方法在字符串中用某些字符替换另一些字符。
代码如下:
var str="Visit cnblogs.com!"
document.write(str.replace(/cnblogs.com/,"Mamogu.com"))
script>