Commonly used methods in js strings 09

Commonly used methods in strings:

Strings are often manipulated in real projects. At this time, we need to master some commonly used string manipulation methods.

I will add a str in front of each method below to represent a string. The purpose is to allow everyone to better understand the methods commonly used in strings.

1. str.charAt() returns the character at the specified index position.
Here we need to pay attention to:

Both str[0] and str.charAt(0) get the first character; when using charAt cannot get a valid character, an empty string is returned. With str[index], when the specified index does not exist, the [] method gets undefined.

2、str.charCodeAt()

在charAt()基础上,把获取的字符变为编码值。‘如果是中文汉字的情况下,转换为Unicode编码值,如果是特殊字符或者字母数字,转换为ASCII编码中的值’。
charCodeAt(0),括号里字符串中字符的索引。
下面有一些常用的数字、字母对应的编码值。
48~57 : 数字 0~9
65~90 : 大写字母 A~Z
97~122 : 小写字母 a~z

3. String.fromCharCode (Unicode value in decimal) //Convert the value to the corresponding character according to the information in the ASCII table.

例:String.fromCharCode(104) 其结果是 h 。在ASCII码表中,十进制114对应的就是小写字母h。
(看到这里的童鞋,不太了解ASCII码表的,请自行问度娘)

4. Str.substr() \ str.substring() \ str.slice()
realizes the method of interception of string,
but the difference is:

str.substr(n,m):从索引n开始,截取m个字符,把找到的部分截取出来。
str.substring(n,m):从索引n开始截取(包含n),截取到索引m处(但是不包含m),把找到的部分截取出来。
str.slice(n,m) : 用法和substring()用法一样,只不过区别在于slice支持负数做为索引。
在索引为负数的时候,浏览器在处理的时候,使用字符串总长度加上负数的索引,然后按照正数处理操作。
例:str的总长度是46
str.slice(-7,-3) -> slice(46-7,46-3) -> slice(39,43)
在这里值得提的一点是 str.slice(0)、str.substring(0)和str.sub(0) 是把原有字符串内容都截取一份。(相当于字符串的克隆)

在高程3(JavaScript高级程序设计第124页)中提到了对substr()方法和substring()方法的索引出现负数的情况的一些处理。
在substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0;substring()方法是会把所有负数参数都转换为0。

When intercepting strings with the above three methods, we also need to pay attention to some details:
First: if only n is passed, it is equivalent to starting from index n and intercepting to the end of the string.
Second: If the index passed exceeds the maximum limit (exceeding the length of the string), just cut off the part that can be intercepted.
Third: If no parameter is passed, it is equivalent to intercepting the entire string. (All three methods are the result.)

5. str.toUppercase() converts all strings to uppercase.

6. str.toLowercase() converts all strings to lowercase.

7. str.indexOf() / lastIndexOf()
str.indexOf() Get the index of the first occurrence of the current character in the string.
str.lastIndexOf() Gets the index of the last occurrence of the current character in the string.

What we need to pay attention to is: str.indexOf() and str.lastIndexOf() both start detection from the string index 0.
E.g:

var str = ‘hello world!’
str.indexOf(‘w’) => 6 str.lastIndexOf(‘w’) => 6

Still str ='hello world!', str.indexOf('llo') means: the characters found must be the three concatenated llo, we get the index of the first character l.

What we need to pay attention to is: if the character we need to find does not exist in the current string, the return is -1 (as long as it exists, the index range is >=0), then we can verify the current Whether a character is included in the string.

Look at the following code ideas can be more lively.

if (str.indexOf(‘?’)===-1){
// 没有出现过
}

if (str.indexOf(‘?’)>=0){
// 出现过
}

8 、 str.split ()

Split the string into an item in the array according to a certain character, and then store them in the array respectively, which corresponds to the join method in the array. (You can check the join method in the array yourself, I will continue to summarize the common methods in the array in my next blog, hehe.)

var hobbyList = ‘music|movie|eat’;
console.log(hobbyList.split(‘|’)); //结果是[‘music’,’movie’,’eat’]

If there are no characters in the string, the original string will be used as an item in the array and returned.
console.log(hobbyList.split("@")); //The result is ['music|movie|eat']
In fact, the current string is searched for a character that does not exist at all through the split method of the string, and the current string is returned. String.

9、str.replace()

Realizing the replacement of characters
Performing the replace method once can only replace it once. If several of them need to be replaced, we need to perform multiple times without using regularization.
In many cases, even if we perform the replace many times, it can't be achieved. At this time, we need to process it regularly. (In real projects, replace is generally used with regular rules).

For example: as shown in the following code

var str =”dajiahao”;
str.replace(“dajiawanshang”,”dajia”);
这是str的结果就变成了 ‘dajiawanshanghao’。
str.replace(“dajiawanshang”,”dajia”);
经过两次替换的结果‘dajiawanshangdajiawanshanghao’。
经过我们的两次替换依然没有实现效果。

10 trim / trimLeft / trimRight
str.trimLeft: 去除字符串开始的空格。

str.trimRight:去除字符串结尾的空格。
str.trim: 去除首尾的空格。(不能去除字符串与字符串之间的空格!!!)。

Guess you like

Origin blog.csdn.net/qq_45555960/article/details/102468571