1:length
获取字符串长度
var str = "hello world";
alert(str.length); //11
2:索引
通过索引获取字符串指定位置的字符,但是不能改变该索引对应的值
var str = "hello world"
alert(str[0]); //h
str[0] = "H";//不会影响str,但也不会报错
3:toUpperCase
将字符串全部转化为大写并返回,原字符串不变
var str = "hello world";
alert(str.toUpperCase());
4:toLowerCase
将字符串全部转化为小写并返回,原字符串不变
var str = "hello world";
alert(str.toLowerCase());
5:indexOf
搜索指定字符串出现的位置,接受2个参数,第一个参数是要查找的元素,第二个参数是要查找的位置;如果找到则返回对应元素所在的位置,否则返回-1
var str='hello world'
alert(str.indexOf('world')) //6
alert(str.indexOf('World') //-1
alert(str.indexOf('0')) //4
alert(str.indexOf('0',6)) //从第6位开始查找,返回7
6:lastIndexOf
跟indexOf一样,只不过它从字符串的末尾开始查找,找到返回对应坐标,找不到返回-1。
7:concat
将一个或多个字符串拼接起来,返回拼接到的新的字符串,原字符串不变
var str1 = "hello ";
var res = str1.concat(" world","!");
alert(res);//返回hello world!
alert(str1);//返回hello
8:slice
返回被操作字符的子字符串,原字符串不变,接受两个参数,字符串的起始位置和结束位置,返回的字符串不包含结束位置的字符,第一个参数要小于第二个参数,否则返回”“,若只有一个参数,返回起始位置到字符串结尾的所有字符串,若传递的参数为负数,将传入的负值与字符创的长度相加;返回一个新的数组
var str = "hello world!";
alert(str.slice(3,7));//返回lo w
alert(str.slice(3));//返回lo world!
alert(str.slice(9,5));//返回""
alert(str.slice(-7,-3));//负数与长度相加,即str.slice(5,9)返回 wor
alert(str.slice(5,9));//返回 wor
9:substring
当传入的参数是正数时,substring与slice的功能基本相同,唯一的区别是当第一个参数大于第二个参数时,方法将第二个参数作为截取的起始位置,将第一个参数作为截取的结束位置,并且截取的字符串不包含第一个参数位置对应的值;当传入的参数是负值时,改方法会将所有的负值转化为0;
var str = "hello world!";
alert(str.substring(3,7));//返回lo w
alert(str.substring(3));//返回lo world!
alert(str.substring(9,5));//返回 wor,即str.substring(5,9),不包含第九项
alert(str.substring(-7,-3));//负数与长度相加,即str.substring(0,0)返回""
alert(str.substring(-7,3));//负数与长度相加,即str.substring(0,3)返回hel
10:substr
返回指定位置开始的指定长度的字符串,原来的字符串不变;如果第二个参数缺省就一直截取到字符串结束;当传递的参数为负数时,方法会将负的第一个参数与字符串的长度想加,将负的第二个参数转化为0‘’
返回一个新的字符串
var s = 'hello world!'
alert(s.substr(0, 5));//从索引0开始,截取5个字符串,返回hello
alert(s.substr(7)); //从索引7开始截取,一直到结束,返回orld!
alert(s.substr(-7,3));//负数与长度相加,即str.substr(5,3),返回 wo
alert(s.substr(-7,-3));//负数与长度相加,即str.substr(5,0),返回""
11:split
基于指定的分隔符将一个字符串分割成多个字符串,并将结果存放在一个数组里面;可以传2个参数,第一个参数为分隔符;第一个参数用于指定返回数组的大小;如果省略改参数,则返回整个数组
var s = "1,23,45";
var arr1 = s.split(",");
alert(arr1);//返回数组["1","23","45"]
var arr2 = s.split(",",2);
alert(arr2);//返回数组["1","23"]
12:trim
删除元素前置及其后缀的所有空格,然后返回结果,原数组不变
var str1 = " hello world ";
var str2 = str1.trim();
alert(str1);//返回" hello world "
alert(str2);//返回"hello world"
13:String
将任何类型的数据都转换为字符串
var num= 19; // 19
var myStr = num.toString(); // "19"
14:replace
在字符串中用一些字符替换另外一些字符,或者替换一个与正则表达式匹配的子串
stringObject.replace(regexp/substr,replacement)
第一个参数必须,规定字符串或者要替换的模式的RegExp对象;如果该值是一个字符串,则将他作为要检索的直接量文本模式,而不是首先被转换为RegExp对象;
第二个参数必须,一个字符串值,规定了替换文本或者生成替换文本的函数;
只能替换找到的第一个字符串
var str = "Visit Microsoft!---Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School")) //Visit W3School!---Visit Microsoft!
全局替换,每当 “Microsoft” 被找到,它就被替换为 “W3School”
var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."
document.write(str.replace(/Microsoft/g, "W3School")) //Welcome to W3School! We are proud to announce that W3School
has one of the largest Web Developers sites in the world.