js string的常用方法

(1)indexOf() 返回某个字符在字符串中首次出现的位置(参数1:目标字符,参数2:字符串开始检索的位置(可省略))

let str1 = "abcbbbcdddcccd";
console.log("num->",str1.indexOf("c"));
//num-> 2
console.log("num->",str1.indexOf("c",4));
//num-> 6
console.log("num->",str1.indexOf("k",4));
//num-> -1 不存在返回-1

(2)charAt() 返回在字符串中指定位置的字符(参数1:目标位置)

var Str = "hello world"; 
console.log("str->",Str.charAt(4));
//str-> o

(3)concat() 用于连接两个或多个字符串(参数1:目标字符串,参数2:目标字符串,…),会将所有参数转换成字符串,和运算符“+”连接类似

var s1 = "hello";
var s2 = " world";
var s3 = 48;
console.log("str->",s1.concat(s2));
console.log("str->",s1.concat(s2,s3));
console.log("str->",s1+s2+s3);
//str-> hello world
//str-> hello world48
//str-> hello world48

(4)match() 在目标字符串中检索指定的值,找到一个或多个正则表达式的匹配

var s1 = "1 HonhKong 2 smida 3";
console.log("str->",s1.match("smida"));
console.log("str->",s1.match("Smida"));
console.log("str->",s1.match(/\d+/g));
//str-> ["smida", index: 13, input: "1 HonhKong 2 smida 3", groups: undefined]
//str-> null
//str-> (3) ["1", "2", "3"]

(5) slice() 提取字符串的某个部分,并以新的字符串返回被提取的部分(参数1:抽取片段起始下标,-1表示最后一个字符串,参数2:抽取片段结尾下标)substring(),substr()与其类似,都可返回字符串指定部分,substring()和slice()允许使用负数作为参数。而substr()的参数是字符位置和提取的长度。

var str = "hello happy world!";
console.log(str.slice(6));
console.log(str.substring(6));
console.log(str.substr(6));
//happy world!
//happy world!
console.log(str.slice(-11));
console.log(str.substring(-11));
console.log(str.substr(-11));
//appy world!
//hello happy world!
//appy world!
console.log(str.slice(3,7));
console.log(str.substring(3,7));
console.log(str.substr(3,7));
//lo h
//lo h
//lo happ

(6)split() 用于把一个字符串分割成字符串数组(参数1:通过什么来分割字符,参数2:返回字符长度)

var str = "hello happy world!";
console.log(str.split(""));
//["h", "e", "l", "l", "o", " ", "h", "a", "p", "p", "y", " ", "w", "o", "r", "l", "d", "!"]
console.log(str.split(" "));
//["hello", "happy", "world!"]
console.log(str.split(","));
//["hello happy world!"]
console.log(str.split(" ",2));
//["hello", "happy"]

(7)toLocalLowerCase() 用于把字符串转换成小写,返回一个新的字符串

var str = "HELLO happy world!";
console.log(str.toLocaleLowerCase());
console.log(str.toLowerCase());
//hello happy world!
//hello happy world!

(8)toLocalUpperCase() 用于把字符串转换成小写,返回一个新的字符串

var str = "HELLO happy world!";
console.log(str.toLocaleUpperCase());
console.log(str.toUpperCase());
//HELLO HAPPY WORLD!
//HELLO HAPPY WORLD!

(9)toString() 返回字符串

var arr = new Array();
arr[0] = 1;
arr[2] = 5;
console.log(arr.toString());
//1,,5
var boo = new Boolean(true);
console.log(boo.toString());
//true
var dat = new Date();
console.log(dat.toString());
//Mon Jan 13 2020 11:35:43 GMT+0800 (中国标准时间)
var dat = new Number(1585);
console.log(dat.toString());
//1585

(10) valueOf() 返回String对象的原始值

var dat = new Date();
console.log(dat.valueOf());
//1578886738937
var dat = new String("Hello World");
console.log(dat.valueOf());
//Hello World
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值