String和string
String是一个基本包装类型,string是一个基本数据类型。
JS中的基本包装类型还有Number和Boolean。
<script>
var str=new String("陆俊好帅");
console.log(typeof str);
console.log(str instanceof String);
var str2="哈哈"
console.log(typeof str2);
console.log(str2 instanceof String);
</script>
从上面代码可以看出new出来的是对象类型,直接赋值创建的是基本类型(string)。
常用的方法
indexOf( searchValue ):
searchValue 在字符串中首次出现的位置。lastIndexOf( searchValue ):
searchValue 在字符串中最后出现的位置。charAt( index ):
获取下标为 index 的元素charCodeAt( index ):
获取下标为 index 的元素的ASCII码str[ index ]:
获取指定位置的字符(HTML5新增,仿Java)
<script>
var str="Hello";
console.log(str[1]);
</script>