06-字符串
属性 截取字符串的长度str.length
方法:
charAt()
方法可返回指定位置的字符
concat()
方法用于连接两个或多个字符串
indexOf()
方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写
//indexOf(查找的值,开始的位置)
lastIndexOf()
方法可返回一个指定的字符串值最后出现的位置
includes()
方法用于判断字符串是否包含指定的子字符串 返回布尔型 true false
replace()
方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
//replace(searchValue,newValue) 返回的是新的字符串
split()
方法用于把一个字符串分割成字符串数组 见js文件
substr()
方法可在字符串中抽取从开始下标开始的指定数目的字符
//substr(start,length)
substring()
方法用于提取字符串中介于两个指定下标之间的字符
//substring(from,to)
slice(start, end)
方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
substring和slice的区别见js文件
toLowerCase()
方法用于把字符串转换为小写
toUpperCase()
方法用于把字符串转换为大写
trim()
方法用于删除字符串的头尾空格
js文件:
var str = 'hello wrold';
var str1 = 'monkey ';
//属性 截取字符串的长度
document.write(str.length); //11
//charAt() 方法可返回指定位置的字符
document.write(str.charAt(1)); //e
document.write(str.charAt(str.length-1)); //d 获取最后一个字符
//concat() 方法用于连接两个或多个字符串
var s = str1.concat(str,' welcome'); //monkey hello world welcome
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写
document.write(str.indexOf('o')); //4 匹配成功后返回索引值
document.write(str.indexOf('a')); //-1 没有匹配成功则返回-1
document.write(str.indexOf('o',5)); //8 indexOf(查找的值,开始的位置)
//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置
document.write(str.lastIndexOf('o')); //8
document.write(str.lastIndexOf('o',5)); //4
//replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
//replace(searchValue,newValue) 返回的是新的字符串
var s = str.replace('hello','hi,'); //hi,world
//split() 方法用于把一个字符串分割成字符串数组
var str3 = 'how,are,you';
document.write(str3.split(",")); // ["how", "are", "you"]
document.write(str3.split(",",2)); // ["how", "are"] 2表示返回数组的最大长度
//substr() 方法可在字符串中抽取从开始下标开始的指定数目的字符
document.write(str.substr(4)); //o wrold
document.write(str.substr(2,4));//substr(start,length) "llo "
//substring() 方法用于提取字符串中介于两个指定下标之间的字符
document.write(str.substring(4)); //o wrold
document.write(str.substring(2,4)); //substr(from,to) ll 不包括to
//slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
document.write(str.slice(2,4)); //ll
document.write(str.slice(-1)); //d -1表示最后一个字符串
document.write(str.substring(-1)); //-1 表示0 hello world
//slice()和substring()区别 思考题
/* var str="abcdefghijkl";
console.log(str.slice(3,-4)); //defgh
console.log(str.substring(3,-4)); //abc*/
//toLowerCase() 方法用于把字符串转换为小写
//toUpperCase() 方法用于把字符串转换为大写
//trim() 方法用于删除字符串的头尾空格
var str5 = ' hello ';
document.write(str5.trim()); //hello