javascript之字符串常用方法学习 charAt concat indexOf substring substr toUpperCase

str=new String("HelloWorldWorld");
console.log("1: "+str.charAt(1));  //返回字符串中指定位置的字符
console.log("2: "+str.charCodeAt(1));  //返回指定位置字符的Unicode编码
str1="nihaoshijie";
result=str.concat(str1);  //concat连接两个字符串
console.log("3: "+result);
console.log("4: "+result.fixed());  //以打印机文本显示字符串
console.log("5: "+result.fontcolor('Red'));  //以指定的颜色显示字符串
result=str.indexOf('World');  //返回子串World在主串str中的位置
console.log("6: "+result);
result=str.lastIndexOf('World');  //从后向前查找子串World在主串str中的位置
console.log("7: "+result);
str2="How are you";
result=str2.split(" ");  //使用“ ”将字符串分割为数组
console.log("8: "+result);
console.log("9: "+str2.sub());  //将字符串显示为下标
console.log("10: "+str2.sup());  //将字符串显示为上标
console.log("11: "+str2.substring(4));  //截取字符串,从start到end,与slice的区别是不能使用负值作为参数
console.log("12: "+str2.substr(4,3));  //截取字符串,从start开始,长度为3
console.log("13: "+str2.toUpperCase());  //将字符串转化为大写
console.log("14: "+str2.toLowerCase());  //将字符串转化为小写
------老子是要成为前端工程师的男人!------

猜你喜欢

转载自blog.csdn.net/Ibelievesunshine/article/details/80582954