JS String(字符串)对象 Boolean对象

更详细信息参考
http://www.w3school.com.cn/example/jsrf_examples.asp
http://www.w3school.com.cn/jsref/jsref_obj_string.asp
1.获取字符串长度str.length

var str = "hello world!";
document.write("字符串str的长度==" + str.length + "<br />"); 

2.返回文本在字符串中首次出现的位置 indexOf()

var str = "hello world!";
document.write("Hello在字符串str中的位置==" + str.indexOf("Hello") + "<br />")

//可以用来判断是否包含子串 
if (str.indexOf("hello") >= 0) {
    document.write("str中包含子字符串hello <br />");
}

3.查找字符串中特定字符,若找到,返回该字符。否则,返回null

var str = "hello world!";
document.write(str.match("world") + "<br />") 
document.write(str.match("World") + "<br />")

4.替换字符串中的字符

var str = "hello world!";
document.write(str.replace(/world/, "W3School"))

5.替换字符串中的所有匹配字符

var str = "hello world!hello world!hello world!";
document.write(str.replace(/world/g, "W3School"))

Boolean(布尔)对象

<script type="text/javascript">
var b1=new Boolean( 0)
var b2=new Boolean(1)
var b3=new Boolean("")
var b4=new Boolean(null)
var b5=new Boolean(NaN)
var b6=new Boolean("false")

document.write("0 是逻辑的 "+ b1 +"<br />")
document.write("1 是逻辑的 "+ b2 +"<br />")
document.write("空字符串是逻辑的 "+ b3 + "<br />")
document.write("null 是逻辑的 "+ b4+ "<br />")
document.write("NaN 是逻辑的 "+ b5 +"<br />")
document.write("字符串 'false' 是逻辑的 "+ b6 +"<br />")
</script>
posted on 2017-03-02 15:03  dreamstar  阅读(68)  评论(0编辑  收藏  举报