1. String conversion
String conversion is the most basic requirement and work, you can convert any type of data to a string, you can use any of the following three methods:
var num= 19; var mystr = num.tostring; "19"
You can also do this:
var num= 19; var mystr = String (num); "19"
Or, just a little bit more simple:
2. String segmentation
String segmentation, a string is split 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 of Split, which represents the maximum length of the returned string array.
3. Get the string length
The string length is often used in development, and is very simple as follows:
var mystr = "I,love,you,do,you,love,me"; var mystrlength = mystr.length; 25
4. Query substring
Many people will forget the JavaScript's own methods, or forget their specific usage, resulting in the need to nest a for loop to do the problem.
The first function: IndexOf, which starts from the beginning of the string, finds the corresponding coordinates, 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 0, cannot find return-1
The second function: LastIndexOf, which starts from the end of the string and finds the corresponding coordinates returned, cannot find return-1. As follows:
var mystr = "I,love,you,do,you,love,me"; var index = Mystr.lastindexof ("You"); 14
The above two functions also receive the second optional parameter, indicating where to start the lookup.
5. String substitution
Simply find that the string should not stop, and general topics often encounter strings that you can find and replace yourself with, for example:
var mystr = "I,love,you,do,you,love,me"; var replacedstr = Mystr.replace ("Love", "hate");//"I,hate,you,do,you,love,me"
By default, only the first lookup is replaced, and if you want to replace it globally, you need to place a 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"
For more detailed information, refer to: http://www.w3school.com.cn/jsref/jsref_replace.asp
6. Find the character or its character encoding value for a given position
To find the character at a given location, you can use the following function:
var mystr = "I,love,you,do,you,love,me"; var TheChar = Mystr.charat (8);//"O", also starting from 0
Similarly, one of its sibling functions is to find the character encoding value for the corresponding location, such as:
var mystr = "I,love,you,do,you,love,me"; var TheChar = mystr.charcodeat (8); 111
7. String connection
The string join operation can be done simply by 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! "
Similarly, JavaScript has its own 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? "
Where the concat function can have multiple arguments, pass multiple strings, and stitch multiple strings together.
8. String cutting and extraction
There are three ways to extract and cut from a string, such as:
First, use splice:
var mystr = "I,love,you,do,you,love,me"; var subStr = Mystr.slice (1,5);//", Lov"
Second, use substring:
var mystr = "I,love,you,do,you,love,me"; var subStr = mystr.substring (1,5); ", Lov"
Third, use substr:
var mystr = "I,love,you,do,you,love,me"; var subStr = mystr.substr (1,5); ", Love"
Unlike the first and second, the substr second parameter represents the maximum length of the truncated string, as shown in the results above.
9. String Case Conversion
The usual conversions are uppercase or lowercase string functions, 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"
10. String Matching
String matching may require you to have a certain understanding of regular expressions, first look at the match function:
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
As you can see, the match function is called on a string and accepts a regular parameter. To take a look at the second example, use 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 put the regular and the string in a different position, that is, the EXEC function is called on the regular, passing string parameters. For the above two methods, the result of the match is a string that returns the first successful match, or null if the match fails.
Let's look at a similar approach to search, such as:
var mystr = "I,love,you,do,you,love,me"; var pattern =/love/; var result = Mystr.search (pattern);//2
Returns only the matched subscripts that were found, and returns 1 if the match fails.
11. String comparison
Compares two strings, compared with rules that are compared 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
12. Example
Finally, we look at a front-end pen question, where to network, I believe many children have done this problem. Title: Write a getsuffix function to get the suffix name of the input parameter, such as input abcd.txt, return txt. Attached to my answer:
function Getsuffix (file) {return File.slice (File.lastindexof (".") + 1,file.length);}
Conclusion
It is believed that there are more than one function of string manipulation in JavaScript, but these should all be very common. If there is any need to add, welcome to add! Hope to see these later, face the string of written test face questions you can very calmly face.