| 【零基础学习web前端】javascript 字符串(String) 对象 
  
  
 String对象:可用于处理或格式化文本字符串以及确定和定位字符串中的子字符串。
 一个字符串用于存储一系列字符就像 "John Doe".
 一个字符串可以使用单引号或双引号:
 
 声明字符串的方式:
 第一种: var s="";
 第二种: var s1=new String("");
 
 实例
 
 [JavaScript] 纯文本查看 复制代码 var carname="Volvo XC60";
var carname='Volvo XC60'你使用位置(索引)可以访问字符串中任何的字符:
 
 [JavaScript] 纯文本查看 复制代码 var character=carname[7];字符串(String)
 字符串(String)使用长度属性length来计算字符串的长度:
 
 [JavaScript] 纯文本查看 复制代码 var txt="Hello World!";
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);在字符串中查找字符串
 字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置:
 
 [JavaScript] 纯文本查看 复制代码 var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");如果没找到对应的字符函数返回-1
 lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。
 
 内容匹配
 match()函数用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符
 
 [JavaScript] 纯文本查看 复制代码 var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));替换内容
 replace() 方法在字符串中用某些字符替换另一些字符。
 
 [JavaScript] 纯文本查看 复制代码 str="Please visit Microsoft!"
var n=str.replace("Microsoft","Runoob");字符串大小写转换
 字符串大小写转换使用函数 toUpperCase() / toLowerCase():
 
 [JavaScript] 纯文本查看 复制代码 var txt="Hello World!";       // String
var txt1=txt.toUpperCase();   // txt1 文本会转换为大写
var txt2=txt.toLowerCase();   // txt2 文本会转换为小写字符串转为数组
 字符串使用split()函数转为数组:
 
 [JavaScript] 纯文本查看 复制代码 txt="a,b,c,d,e"   // String
txt.split(",");   // 使用逗号分隔
txt.split(" ");   // 使用空格分隔
txt.split("|");   // 使用竖线分隔 
 
 |