js字符串方法总结
来源:互联网 发布:nba2k17人物数据 编辑:程序博客网 时间:2024/06/06 01:36
字符串的长度
使用length属性可以查看字符串的长度
转化为字符串
toString()
使用对象,数值,布尔值和字符串值都有该方法。但是null,undefined方法没有
let test1 = [1,2,4];console.log(test1.toString()); // 1,2,4let test2 = 3.3;console.log(test2.toString()); // 3.3let test3 = false;console.log(test3.toString()); // falselet test4 = 'aaa';console.log(test4.toString()); // aaalet test5 = null;console.log(test5.toString()); //errorlet test6 = undefined;console.log(test6.toString()); // error
String()
String()方法可以将任何类型转换为字符串,包括null,undefined。
let test1 = [1,2,4];console.log(String(test1)); // 1,2,4let test2 = 3.3;console.log(String(test2)); // 3.3let test3 = false;console.log(String(test3)); // falselet test4 = 'aaa';console.log(String(test4)); // aaalet test5 = null;console.log(String(test5)); //nulllet test6 = undefined;console.log(String(test6)); // undefined
查找字符方法
charAt()
查找某个位置的字符。
let stringValue = "hello world";console.log(stringValue.charAt(1)); // econsole.log(stringValue.charAt(20)); // 没有,为空console.log(stringValue.charAt(-1)); // 一样是空
charCodeAt()
查找某个位置的字符编码。
let stringValue = "hello world";console.log(stringValue.charCodeAt(1)); // 101console.log(stringValue.charCodeAt(-1)); // NaNconsole.log(stringValue.charCodeAt(20)); // NaN
使用数字索引访问
let stringValue = "hello world";console.log(stringValue[1]); // econsole.log(stringValue[20]); // undefinedconsole.log(stringValue[-1]); // undefined
操作字符串
concat()
用于拼接字符串,返回新的字符串。不改变原字符串。
let stringValue = "hello ";let result = stringValue.concat("world");console.log(result); //hello worldconsole.log(stringValue); //hello
也可以直接使用“+”符号来操作字符串。
slice()和substr()和subString()
这三个字符串都是基于子字符串创建新字符串的方法。
都包含两个参数:
- 第一个表示子字符串的位置;
- 第二个参数可选。slice()函数和subString()函数的第二个参数表示子字符串最后一个字符后面的位置。substr()字符串表示返回的字符个数。
let stringValue = "hello world";console.log(stringValue.slice(3)); // lo worldconsole.log(stringValue.slice(3,7)); // lo wconsole.log(stringValue.substr(3)); // lo worldconsole.log(stringValue.substr(3,7)); // lo worlconsole.log(stringValue.substring(3)); // lo worldconsole.log(stringValue.substring(3,7)); // lo w
在参数为负值的情况下,参数有很大的不同。
- slice()方法将传入的负值与字符串长度相加
- substr()方法将负的第一个参数加上字符串长度,负的第二个参数转换为0
- substring()方法会把所有负值都转换为0
let stringValue = "hello world";console.log(stringValue.slice(-3)); // rldconsole.log(stringValue.slice(-3,-4)); // 空console.log(stringValue.substr(-3)); // rldconsole.log(stringValue.substr(-3,-4)); // 空console.log(stringValue.substring(-3)); // hello worldconsole.log(stringValue.substring(-3,-4)); // 空
字符串位置方法
indexOf()
从前向后查找字符所在的位置。第二个参数可选,表示从指定位置向后查找。
let stringValue = "hello world";console.log(stringValue.indexOf("o")); // 4console.log(stringValue.indexOf("o", 6)); // 7
lastIndexOf()
从后向前查找字符所在位置。第二个参数可选,表示从指定位置向前查找。
let stringValue = "hello world";console.log(stringValue.lastIndexOf("o")); // 7console.log(stringValue.lastIndexOf("o", 6)); // 4
trim()
删除字符串前后空格。并且不影响原字符串。
let stringValue = " hello world ";let trimStringValue = stringValue.trim();console.log(stringValue); // hello world console.log(trimStringValue); //hello world
字符串大小写转换
toLowerCase()
将字符串所有字符转换为小写字符。
let stringValue = "Hello World";console.log(stringValue.toLowerCase()); // hello world
toLocaleLowerCase()
针对特定地区将字符串所有字符转换为小写字符。
let stringValue = "Hello World";console.log(stringValue.toLocaleLowerCase());// hello world
toUpperCase()
将字符串所有字符转换为大写字符。
let stringValue = "Hello World";console.log(stringValue.toUpperCase());// HELLO WORLD
toLocaleUpperCase()
针对特定地区将字符串所有字符转换为大写字符。
let stringValue = "Hello World";console.log(stringValue.toLocaleUpperCase());// HELLO WORLD
模式匹配方法
match()
匹配符合条件的字符串。只接受一个参数,正则表达式或者RegExp对象。匹配结果为一个数组。
let text = "cat, bat, sat, fat";let pattern = /.at/;let matches = text.match(pattern);console.log(matches); // ["cat", index: 0, input: "cat, bat, sat, fat"]console.log(matches.index); // 0console.log(matches[0]); // catconsole.log(pattern.lastIndex); // 0
search()
返回字符串中第一个匹配项的索引。若没有,返回-1。从前向后查找字符串。其接受的参数与match相同。
let text = "cat, bat, sat, fat";let pattern = /.at/;console.log(text.search(pattern)); // 0
replace()
替换匹配到的相应字符串。第一个参数可以是正则表达式,也可以是字符串,第二个参数可以是字符串或函数。一般情况下只会替换匹配的第一个字符串,若要全局替换,需要使用正则表达式的全局标志。
let text = "cat, bat, sat, fat";console.log(text.replace("at", "ond"));// cond, bat, sat, fatconsole.log(text.replace(/at/g, "ond")); // cond, bond, sond, fond
split()
基于指定分隔符将字符串拆分为数组。第一个参数表示指定的分隔符,可以是字符串或正则表达式;第二个参数可选,表示数组长度。
let colorText = "red,blue,green,yellow";console.log(colorText.split(','));// ["red", "blue", "green", "yellow"]console.log(colorText.split(',',2));// ["red", "blue"]console.log(colorText.split(/,/));// ["red", "blue", "green", "yellow"]
字符串比较方法
localeCompare()
比较两个字符串,根据返回值确定大小。
let stringValue = "yellow";console.log(stringValue.localeCompare("brick")); // 1console.log(stringValue.localeCompare("yellow")); // 0console.log(stringValue.localeCompare("zoo")); // -1
字符编码转字符
fromCharCode()
接受一个或多个字符编码,将它们转换成一个字符串。
console.log(String.fromCharCode(104,101,108,108,111));// hello
阅读全文
0 0
- js字符串方法总结
- js字符串编码方法总结
- js字符串方法(总结)
- JS字符串常用方法总结
- js字符串常用方法总结
- js中的字符串方法总结
- js字符串基本方法总结
- JS字符串方法总结及字符串增删查改方法
- js对字符串进行编码方法总结
- JS常用字符串处理方法总结
- js对字符串进行编码方法总结
- [js点滴]JavaScript字符串常见方法总结
- 总结一下JS数组、字符串的方法
- js把字符串转换成int类型的方法总结
- js字符串操作总结
- js字符串处理总结
- js中字符串方法
- JS截取字符串方法
- Ucloud 配置haproxy+keepalived (利用ucloudAPI实现外网浮动IP切换)
- windows下Mingw(GCC) 编译miniupnpc-1.8
- 关于头文件
- 使用 Intent传递 Bean
- Java提高篇(三三)-----Map总结
- js字符串方法总结
- Effective STL 21 Always have comparison functions return false for equal values
- JQUERY MINIUI
- 从cinder 删除volume 分析cinder rpc
- 数学公式
- docker容器limit和inode问题
- 只为积分
- ViewTreeObserver
- jquery 日期