JavaScript 基础之String对象(六)
2018-09-25 本文已影响0人
桜花約束
- 使用位置(索引)可以访问字符串中任何的字符:
var character=carname[7];- 使用长度属性length来计算字符串的长度:
var txt="Hello World!";
document.write(txt.length);- 使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置:
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");- match()函数查找字符串中特定的字符,并且找到的话返回这个字符。
var str="Hello world!";
document.write(str.match("world") + "
");
document.write(str.match("World") + "
");
document.write(str.match("world!"));- replace() 方法在字符串中用某些字符替换另一些字符。
str="Please visit Microsoft!"
var n=str.replace("Microsoft","Runoob");
-使用函数 toUpperCase() / toLowerCase()进行字符大小写转换:
var txt="Hello World!"; // String
var txt1=txt.toUpperCase(); // txt1 文本会转换为大写
var txt2=txt.toLowerCase(); // txt2 文本会转换为小写- 使用split()函数转为数组:
txt="a,b,c,d,e" // String
txt.split(","); // 使用逗号分隔
txt.split(" "); // 使用空格分隔
txt.split("|"); // 使用竖线分隔- 使用反斜线(\)插入特殊符号
- 字符串属性:
length
prototype
constructor- 字符串方法:
charAt()
charCodeAt()
concat()
fromCharCode()
indexOf()
lastIndexOf()
match()
replace()
search()
slice()
split()
substr()
substring()
toLowerCase()
toUpperCase()
valueOf()