Common string manipulation functions and usage summaries in JavaScript

  • 2020-06-03 05:50:36
  • OfStack

This article summarizes common string manipulation functions and their usage in JavaScript. Share to everybody for everybody reference. The specific analysis is as follows:

Recently, I took the written test of front-end intern recruitment for several times, and found that a lot of written test questions, such as qunar writing test, Taobao writing test and so on, will be tested on string processing. If you often take the written test or are experienced, I believe you are also like me, and found that string processing is one of the most common types of questions in the front-end recruitment process. These questions have one characteristic. From the perspective of the examiner, what it tests is not whether you can, but whether you can write the answer in a concise way without borrowing the XX manual or XX guide or baidu Google. Unfortunately, many developers, and I'm one of them, have trouble remembering their usage for many of the string manipulation functions they use all the time and have to turn to the XX manual or the XX guide or baidu Google. As a result, these critical basics are not robust enough to be traversed by N nested for loops. This is a sign that when you find yourself using too many for loops for this type of problem, be aware that you are probably biased. Don't look down on these things, they may play a big role in finding a job and developing in general. All right, without further ado, let's take them one by one. Inevitably there are omissions, if you happen to find out, welcome to add or private messages.

1, string conversion

String conversion is the most basic requirement and work. You can convert any type of data to a string. You can do this in any of the following three ways:


var num= 19; // 19
var myStr = num.toString(); // "19"

You can also do this:


var num= 19; // 19
var myStr = String(num); // "19"

Or, more simply:


var num= 19; // 19
var myStr = "" +num; // "19"

2. String segmentation

String segmentation is the splitting of a string into multiple strings. JavaScript provides us with a very convenient function, such as:


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"];

The second argument to split(), representing the maximum length of the string array returned.

Get the length of the string

String lengths are commonly used in development and are as simple as this:


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

4, query substring

Many people forget the JavaScript's built-in methods or how to use them, so that they have to be nested within the for loop.

The first function: indexOf(), which looks from the beginning of the string, finds the coordinate that returns the corresponding coordinate, and cannot find the return -1. As follows:


var myStr = "I,Love,you,Do,you,love,me";
var index = myStr.indexOf("you"); // 7 , Based on the 0 start , Unable to return -1

The second function, lastIndexOf(), starts at the end of the string, finds the corresponding coordinate to return, and cannot find the return -1. As follows:


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

Both of these functions also receive an optional second argument, indicating where to start the search.

5. String substitution

Just looking up a string should not stop. In general, you will often come across a string that asks you to look up and replace it with your own. For example:


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

The default is to replace only what is found in the first time. If you want global replacement, you need to set the regular global identity, 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"

More explanation, refer to: https: / / www ofstack. com w3school/js/jsref_replace htm

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

To find characters 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", From the same 0 start 

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


var num= 19; // 19
var myStr = String(num); // "19"
0

String concatenation

String concatenation operations can be as simple as using 1 addition operator, such as:


var num= 19; // 19
var myStr = String(num); // "19"
1

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


var num= 19; // 19
var myStr = String(num); // "19"
2

The concat() function can take multiple arguments, pass multiple strings, and concatenate multiple strings.

8. String cutting and extraction

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

The first, using slice():


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

The second, using substring():


var num= 19; // 19
var myStr = String(num); // "19"
4

Third, using substr():


var num= 19; // 19
var myStr = String(num); // "19"
5

Unlike the first and second, the second parameter of substr() represents the maximum length of the intercepted string, as shown above.

9, string capitalization conversion

Commonly used to convert to uppercase or lowercase string functions, as follows:


var num= 19; // 19
var myStr = String(num); // "19"
6

10. String matching

String matching may require you to have a definite knowledge of regular expressions. Take a look at the match() function:


var num= 19; // 19
var myStr = String(num); // "19"
7

As you can see, the match() function is called on a string and takes a regular argument. 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

Simply switch the regular and string positions, that is, the exec() function is called on the regular to pass a string argument. For both of the above methods, the result of the match is to return the first string with a successful match and null if the match fails.

Let's look at a similar method search(), for example:


var num= 19; // 19
var myStr = String(num); // "19"
9

Returns only the subscript of the found match, or -1 if the match fails.

11. String comparison

To compare two strings, a rule is to compare them alphabetically, as in:


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

12, for example,

Finally, let's take a look at the 1 front pen test, qunar.com, I believe many children have done this problem. Write an getSuffix function to get the suffix name of the input parameter. For example, enter ES147en.txt and return txt. Attached is my answer:


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

conclusion

Believe that there are more string manipulation functions in JavaScript, but the ones listed above should be very common. If there is any need to add, welcome to add! I hope that after seeing these, you can face the written and interview questions of string very calmly.

I hope this article has been helpful for your javascript programming.


Related articles: