JavaScript字符串方法

字符串的方法

charAt() 输入下标返回指定字符

var stringValue = "hello world";
alert(stringValue.charAt(1));   //"e"

charCodeAt() 输入下标返回unicode编码 返回值为 0 - 65535 之间的整数

var stringValue = "hello world";
alert(stringValue.charCodeAt(1)); //输出"101"

[i] 加方括号访问指定字符

var stringValue = "hello world";
alert(stringValue[1]);   //"e"

concat() 将一或多个字符串拼接起来,返回拼接得到的新字符串 (可以使用加号操作符代替更快捷)

var stringValue = "hello ";
var result = stringValue.concat("world", "!");//连接两个字符
alert(result);//"hello world!"

slice()、substr()和 substring()
正值时第一个参数指定子字符串的开始位置
slice()和 substring()的第二个参数指定的是子字符串最后一个字符后面的位置 不包括自身。
substr()的第二个参数指定的则是返回的字符个数 不包括自身。没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。

var stringValue = "hello world";
alert(stringValue.slice(3));//"lo world"
alert(stringValue.substring(3));//"lo world"
alert(stringValue.substr(3));//"lo world"
alert(stringValue.slice(3, 7));//"lo w"
alert(stringValue.substring(3,7));//"lo w"   
alert(stringValue.substr(3, 7));//"lo worl" 下标3开始截取7个

负值

var stringValue = "hello world";
alert(stringValue.slice(-3));//"rld" //从右往左取 3个下标1开始
alert(stringValue.substring(-3));//"hello world"
alert(stringValue.substr(-3)); //"rld"/从右往左取 3个下标1开始
alert(stringValue.slice(3, -4));//"lo w"  
alert(stringValue.substring(3, -4));//"hel" 
alert(stringValue.substr(3, -4));//""(空字符串)

indexOf() 输入查找字符串最先出现位置的下标(从左开始搜索)
lastindexOf() 输入查找字符串最先出现位置的下标(从右开始搜索)
如果没有找到该子字符串,则返回-1
如果一个字符串中仅出现了一次,那么 indexOf()和 lastIndexOf()会返回相同的位置值

var stringValue = "hello world";
alert(stringValue.indexOf("o"));             //4 
alert(stringValue.lastIndexOf("o"));         //7

var stringValue = "hello world";
alert(stringValue.indexOf("o", 6));         //7 从下标6开始搜索往右第一个出现的o
 alert(stringValue.lastIndexOf("o", 6));     //4 从下标6开始搜索往左第一个出现的o

trim() 创建一个字符串的副本,删除字符串开头结尾的所有空格,然后返回结果。

var stringValue = "   hello world   ";
 var trimmedStringValue = stringValue.trim();
 alert(stringValue);            //"   hello world   "
alert(trimmedStringValue);     //"hello world"

toLowerCase()和 toUpperCase() 大小写转换
oLocaleLowerCase()和 toLocaleUpperCase() 大小写转换 特定地区的实现大小写转换

var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase());  //"HELLO WORLD"
alert(stringValue.toUpperCase());        //"HELLO WORLD"
 alert(stringValue.toLocaleLowerCase());  //"hello world"
 alert(stringValue.toLowerCase());        //"hello world"

字符串的模式匹配方法

match() 输入只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象。

var text = 'cat,bat,dat,lat';
var pattern = /.at/; 
//等价于pattern.exec(text) == text.match(pattern);

var matches = text.match(pattern); 
alert(matches.index); //0 
alert(matches[0]); //"cat" 
 alert(pattern.lastIndex); //0

search() 返回字符串中第一个匹配项的索引;如果没有找到匹配项,则返回-1。 而且,search()方法始终是从字符串开头向后查找模式。只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象。

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos);   //1

replace()替换数组
第一个参数可以是一个 RegExp 对象或者一个字符串(这个字符串不会被转换成正则表达式)
第二个参数可以是一个字符串或者一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。
想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(g)标志

n 匹配第n个捕获组的子字符串,其中n等于0~9。例如,1是匹配第一个捕获组的子字符串(一般就是字符串本身),2是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串nn 匹配第nn个捕获组的子字符串,其中nn等于01~99。例如,01是匹配第一个捕获组的子字符串,02 是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串

var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
alert(result);    //"cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
alert(result);    //"cond, bond, sond, fond"

如果第二个参数是字符串 可以使用特殊字符 nnn

var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word ($1)");//$1是表示第一个捕获组的子字符串 
alert(result);    //word (cat), word (bat), word (sat), word (fat)

split() 基于指定的分隔符将一个字符串分割成多个子字符串,分隔符可以是字符串,也可以是一个 RegExp 对象(这个方 法不会将字符串看成正则表达式)。split()方法可以接受可选的第二个参数,用于指定数组的大小, 以便确保返回的数组不会超过既定大小。

var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");//["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2);//["red", "blue"]
var colors3 = colorText.split(/[^\,]+/);//["", ",", ",", ",", ""]

localeCompare() 比较两个字符串大小,并返回值中的一个

var stringValue = "yellow"; 
alert(stringValue.localeCompare("brick")); //1  因为"brick"在 字母表中排在"yellow"之前,所以 localeCompare()返回了 1
alert(stringValue.localeCompare("yellow")); //0  字符串相等返回0
lert(stringValue.localeCompare("zoo")); //-1  因为"zoo"在字母表中排在"yellow"后面,所以 localeCompare() 返回了-1

String 构造函数本身还有一个静态方法fromCharCode()
fromCharCode()接收一或多个字符编码,然后将它们转换成一个字符串。//与 charCodeAt()用法相反

alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容