创建:使用单引号或者双引号:
let str = '字符串';
可以通过索引访问字符串:str[i]
属性:length:长度
方法;
indexOf(value):定位字符串首次出现的位置。没有找到的话,返回-1。
lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
内容匹配方法:match();
用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。没有找到的话,返回null.
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
替换内容:
replace()字符串中用某些字符替换另一些字符。
str="Please visit Microsoft!"
var n=str.replace("Microsoft","w3cschool");
如果查找不到,就不会发生替换 。
字符串大小写转换
字符串大小写转换使用函数 toUpperCase() / toLowerCase():
var txt="Hello World!"; // String
var txt1=txt.toUpperCase(); // txt1 is txt converted to upper
var txt2=txt.toLowerCase(); // txt2 is txt converted to lower