Common string manipulation functions and usage in JavaScript

伊谢尔伦
Release: 2016-11-24 14:57:53
Original
1318 people have browsed it

1. String conversion

String conversion is the most basic requirement and work. You can convert any type of data into a string. You can use any of the following three methods:

var num= 19; // 19 var myStr = num.toString; // "19"
Copy after login

You are the same You can do this:

var num= 19; // 19 var myStr = String(num); // "19"
Copy after login

Or, even simpler:

2. String splitting

String splitting is to split a string into multiple strings. JavaScript provides us with a very convenient function , such as: The second parameter of

var myStr = "I,Love,You,Do,you,love,me"; var substrArray = myStr .split(","); 
// ["I", "Love", "You", "Do", "you", "love", "me"]; var arrayLimited = myStr .split(",", 3); // ["I", "Love", "You"];
Copy after login

split represents the maximum length of the returned string array.

3. Get the string length

The string length is often used in development. It is very simple as follows:

var myStr = "I,Love,You,Do,you,love,me"; var myStrLength = myStr.length; //25
Copy after login

4. Query substring

Many people will forget these JavaScripts. methods, or forget their specific usage, resulting in having to nest a for loop when doing the questions.

The first function: indexOf, it searches from the beginning of the string and returns the corresponding coordinates if it is found. If it is not found, it returns -1. As follows:

var myStr = "I,Love,you,Do,you,love,me"; var index = myStr.indexOf("you"); // 7 ,基于0开始,找不到返回-1
Copy after login

The second function: lastIndexOf, it searches from the end of the string and returns the corresponding coordinates if found. If not found, it returns -1. As follows:

var myStr = "I,Love,you,Do,you,love,me"; var index = myStr.lastIndexOf("you"); // 14
Copy after login

The above two functions also receive a second optional parameter, indicating the starting position of the search.

5. String replacement

Just finding the string should not stop. In general questions, you will often be asked to find and replace it with your own string, for example:

var myStr = "I,love,you,Do,you,love,me"; var replacedStr = myStr.replace("love","hate");//"I,hate,you,Do,you,love,me"
Copy after login

The default is to only replace If you want to replace it globally when you find it for the first time, you need to put a regular global identifier, such as:

var myStr = "I,love,you,Do,you,love,me"; var replacedStr = myStr.replace(/love/g,"hate");//"I,hate,you,Do,you,hate,me"
Copy after login

For more details, please refer to: http://www.w3school.com.cn/jsref/jsref_replace.asp

6. Find the character at a given position or its character encoding value

To find the character at a given position, you can use the following function:

var myStr = "I,love,you,Do,you,love,me"; var theChar = myStr.charAt(8);// "o",同样从0开始
Copy after login

Similarly, one of its sibling functions is to find the character encoding at the corresponding position Value, such as:

var myStr = "I,love,you,Do,you,love,me"; var theChar = myStr.charCodeAt(8); //111
Copy after login

7. String connection

The string connection operation can be as simple as using an addition operator, such as:

var str1 = "I,love,you!"; var str2 = "Do,you,love,me?"; var str = str1 + str2 + "Yes!";//"I,love,you!Do,you,love,me?Yes!"
Copy after login

Similarly, JavaScript also comes with related functions, such as:

var str1 = "I,love,you!"; var str2 = "Do,you,love,me?"; var str = str1.concat(str2);//"I,love,you!Do,you,love,me?"
Copy after login

The concat function can have multiple parameters, pass multiple strings, and splice multiple strings.

8. String cutting and extraction

There are three ways to extract and cut from strings, such as:

The first one, use splice:

var myStr = "I,love,you,Do,you,love,me"; var subStr = myStr.slice(1,5);//",lov"
Copy after login

The second one, use substring:

var myStr = "I,love,you,Do,you,love,me"; var subStr = myStr.substring(1,5); //",lov"
Copy after login

The third method is to use substr:

var myStr = "I,love,you,Do,you,love,me"; var subStr = myStr.substr(1,5); //",love"
Copy after login

. The difference from the first and second methods is that the second parameter of substr represents the maximum length of the intercepted string, as shown in the above results.

9. String case conversion

Commonly used functions for converting to uppercase or lowercase strings are as follows:

var myStr = "I,love,you,Do,you,love,me"; var lowCaseStr = myStr.toLowerCase;
//"i,love,you,do,you,love,me"; var upCaseStr = myStr.toUpperCase;//"I,LOVE,YOU,DO,YOU,LOVE,ME"
Copy after login

10. String matching

String matching may require you to have a certain understanding of regular expressions. Let’s take a look at the match function first:

var myStr = "I,love,you,Do,you,love,me"; var pattern = /love/; var result = myStr.match(pattern);
//["love"] console.log(result .index);//2 console.log(result.input );//I,love,you,Do,you,love,me
Copy after login

As you can see, the match function is called on a string and accepts a regular parameter. Let’s take a look at the second example, using the exec function:

var myStr = "I,love,you,Do,you,love,me"; var pattern = /love/; var result = pattern .exec(myStr);
//["love"] console.log(result .index);//2 console.log(result.input );//I,love,you,Do,you,love,me
Copy after login

It’s simple. It just changes the position of the regular expression and the string. That is, the exec function is called on the regular expression and passes the parameters of the string. For the above two methods, the matching result is to return the first successfully matched string. If the match fails, null is returned.
Let’s look at a similar method search, such as:

var myStr = "I,love,you,Do,you,love,me"; var pattern = /love/; var result = myStr.search(pattern);//2
Copy after login

Only return the found matching string Subscript, if the match fails, -1 is returned.

11. String comparison

Compare two strings. The comparison rule is to compare in alphabetical order, such as:

var myStr = "chicken"; var myStrTwo = "egg"; var first = myStr.localeCompare(myStrTwo); 
// -1 first = myStr.localeCompare("chicken"); // 0 first = myStr.localeCompare("apple"); // 1
Copy after login

12. Example

Finally we Let’s take a look at a front-end written test question from Qunar.com. I believe many children have done this question. Question: Write a getSuffix function to obtain the suffix name of the input parameter. For example, enter abcd.txt and return txt. Attached is my answer:

function getSuffix(file){ return file.slice(file.lastIndexOf(".") + 1,file.length); }
Copy after login

Conclusion

I believe there should be more than these string manipulation functions in JavaScript, but the ones listed above should all be very commonly used. If there is anything you need to add, please feel free to add it! I hope that after seeing this, you will be able to face the string written interview questions very calmly.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template