String和string

String是一个基本包装类型,string是一个基本数据类型。
JS中的基本包装类型还有Number和Boolean。

  1. <script>
  2. var str=new String("陆俊好帅");
  3. console.log(typeof str);
  4. console.log(str instanceof String);
  5. var str2="哈哈"
  6. console.log(typeof str2);
  7. console.log(str2 instanceof String);
  8. </script>

image.png
从上面代码可以看出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>

image.png