javaScript字符串属性及方法

字符串:

1.charAt(index)

返回字符串中index位置的字符,下标从 0 开始

var myString = 'hello world';
console.log(myString.charAt(3)); // l

2.charCodeAt(index)

返回字符串中index位置字符的unicode值

var myString = 'hello world';
console.log(myString.charCodeAt(3)); // 108

3.concat(item1,item2…)

拼接字符串,不会改变原有字符串,返回新的字符串

var message="l"
var final=message.concat("love","world")
console.log(final) //l love world

4.fromCharCode(item1,item2…)

将Unicode值转换成字符

console.log(String.fromCharCode(72,69,76,76,79))  //HELLO

5.indexOf(substr, [start])

substr为字符串返回找的的字符串当前索引,如果找不到返回-1
start选传,为开始查找的索引位置默认为0

var myString = 'hello world';
myString.indexOf("e") // 1

6.lastIndexOf(substr, [start])

同上不过是最后一个符合字符的索引
start选传,默认为String.length-1

var myString = 'hello world';
myString.lastIndexOf("l") // 9

7.match(regexp)

根据正则表达式在字符串中搜索匹配项。如果没有找到匹配项,则返回一个信息数组或null

var intRegex = /[0-9 -()+]+$/;  

var myNumber = '999';
var myInt = myNumber.match(intRegex);
console.log(isInt);
//output: 999

var myString = '999 JS Coders';
var myInt = myString.match(intRegex);
console.log(isInt);
//output: null

8.replace(regexp/substr, replacetext)

方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

var myString = 'l love world';
console.log(myString.replace(/love /i, "like")); // l like world

name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1"); //"John Doe"

9.search(regexp)

检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,如果找到,返回与 regexp 相匹配的子串的起始位置,否则返回 -1。

var str="hello world"
console.log(str.search(/world/)) // 6

10.slice(start, [end])

提取字符串的某个部分,返回一个新的字符串
从start开始end结束

var text="excellent"
text.slice(0,4) //returns "exce"

11.split(delimiter, [limit])

** 方法用于把一个字符串分割成字符串数组,返回一个字符串数组返回的数组中的字串不包括 delimiter自身**

var str="How are you"
console.log(str.split(" ") + "<br />")
console.log(str.split(" ",3)) //["How", "are", "you"]

12.substr(start, [length])

** substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。**

var text="excellent"
text.substring(0,4) //returns "exce"
text.substring(2,4) //returns "ce"
text.substring(2) //returns "cellent"

13.substring(from, [to])

** 方法用于提取字符串中介于两个指定下标之间的字符**

var str="Hello world"
console.log(str.substring(3))  //lo world
console.log(str.substring(3,7))  //lo w

14.toLowerCase()

** 用于把字符串转换为小写**

var myString = 'L LOVE WORLD';
myString = myString.toLowerCase();
console.log(myString)  //l love world

15.toUpperCase()

** 用于把字符串转换为大写**

var myString = 'l love world';
myString = myString.toLowerCase();
console.log(myString)  //L LOVE WORLD

16.includes()

** 检查字符串是否包含指定的字符串或字符,返回true或者false**

var mystring = "l love world";
var n = mystring.includes("love ");
console.log(n)  //true

17.endsWith()

** 检查字符串是否以指定的字符串或字符结束,返回true或者false**

var mystring = "l love world";
var n = mystring.includes("world");
console.log(n)  //true

18.repeat()

** 构造并返回一个新字符串,被连接在一起的指定数量的字符串的副本**

var mystring = "l love world";
console.log(mystring.repeat(2)); //l love worldl love world

19.valueOf()

** 返回一个String对象的原始值(primitive value),该值等同于String.prototype.toString()。**

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var v=fruits.valueOf();
console.log(v) //["Banana", "Orange", "Apple", "Mango"]

20.trim()

** 删除两端的空白符**

var str = "     Hello Edureka!     ";
console.log(str.trim()); //  Hello Edureka!

新增es6字符串方法

判断开头,结尾

21startsWith()

var str = "asdfg";
console.log(str.startsWith("a"))//返回ture

22endWith()

var str = "asdfg";
console.log(str.startsWith("g"))//返回ture

22.includes

//字符串是否包含某个字符

console.log(str.includes('x'));//true
console.log(str.includes('z'));//false

23.repeat

//repeat()重复某个字符串几次

console.log(str.repeat(3));//lxylxylxy
console.log(str.repeat(5));//lxylxylxylxylxy

猜你喜欢

转载自blog.csdn.net/weixin_44348028/article/details/106290327