Convert string
You can convert an object of a number, a boolean value, or a string:
var myNumber = 24; // 24 var myString = (); // "24"
var myNumber = 24; // 24 var myString = String(myNumber); // "24"
Splitting a string into multiple substrings
To distinguish a string into a substring array, you can use the split() method:
var myString = "coming,apart,at,the,commas"; var substringArray = (","); // ["coming", "apart", "at", "the", "commas"] var arrayLimited = (",", 3); // ["coming", "apart", "at"]
The second parameter of the last row limits the number of items specified by the array parameter.
Get the length of a string
To find out the length attribute of a string that is several characters long:
var myString = "You're quite a character."; var stringLength = ; // 25
Find a substring in a string
There are two ways to do this.
Use indexOf():
var stringOne = "Johnny Waldo Harrison Waldo"; var wheresWaldo = ("Waldo"); // 7
The indexOf() method starts from the beginning of the string to search for the substring (through) the first parameter and returns the start position of the first occurrence of the substring.
Use lastIndexOf():
var stringOne = "Johnny Waldo Harrison Waldo"; var wheresWaldo = ("Waldo"); // 22
The lastIndexOf() method is exactly the same, except that it returns the last occurrence of the substring passed.
In both methods, if no substring is found, the value -1 is returned and an optional second parameter is allowed to indicate the position of the character you want to start searching in the string
Replace a substring
To replace a new string with part or all of the string, you can use replace():
var slugger = "Josh Hamilton"; var betterSlugger = ("h Hamilton", "e Bautista"); (betterSlugger); // "Jose Bautista"
The first parameter is the substring you want to replace, and the second parameter is the new substring. This will only replace the first instance of the matching substring.
To replace all instances of the matching substring, use the global flags of the regular expression:
var myString = "She sells automotive shells on the automotive shore"; var newString = (/automotive/g, "sea"); (newString); // "She sells sea shells on the sea shore"
The second parameter may include a special replacement mode, or may be a function.
At the given location, find the corresponding character
The character to be found is at the specified location, you can use the charAt() method:
var myString = "Birds of a Feather"; var whatsAtSeven = (7); // "f"
Often in the case of JavaScript, the first position in the reference string is "0", not "1".
Alternatively, you can also use charCodeAt() which gives you the character code instead of the character itself:
var myString = "Birds of a Feather"; var whatsAtSeven = (7); // "102" var whatsAtEleven = (11); // "70"
Note that the character code of the bit (11) of the uppercase letter "F" is different from the character code of the bit (7) of the lowercase letter "f".
Concatenate multiple strings
In most cases, when you concatenate a string, you use the addition operator (+). But you can also choose to use the CONCAT() method:
var stringOne = "Knibb High football "; var stringTwo = ("rules."); // "Knibb High football rules"
You can also add multiple strings one by one (in the order in which they appear):
var stringOne = "Knibb "; var stringTwo = "High "; var stringThree = "football "; var stringFour = "rules."; var finalString = (stringTwo, stringThree, stringFour); (finalString); // "Knibb high football rules."
Extract string (compose a new string)
There are three different ways to create a new string value:
Use slice() method:
var stringOne = "abcdefghijklmnopqrstuvwxyz"; var stringTwo = (5, 10); // "fghij"
Use substring() method:
var stringOne = "abcdefghijklmnopqrstuvwxyz"; var stringTwo = (5, 10); // "fghij"
The first argument for these two slice() and substring() methods is the substring you want to start, and the second argument (which is optional) after the end of the string with the character in the string. Therefore, in the above example, the parameter "5, 10" refers to characters 5 to 9 to create a new string.
Using SUBSTR()
var stringOne = "abcdefghijklmnopqrstuvwxyz"; var stringTwo = (5, 10); // "fghijklmno"
SUBSTR() , the first parameter represents the character that starts the new string, and the second parameter is optional. But at this time, the total number of characters represented by the second parameter should include the position of the starting character "5".
Convert a string to uppercase or lowercase
There are four ways to convert case. There are two strings converted to all capitals:
var stringOne = "Speak up, I can't hear you."; var stringTwo = (); // "SPEAK UP, I CAN'T HEAR YOU" var stringThree = (); // "SPEAK UP, I CAN'T HEAR YOU"
Convert string to lowercase:
var stringOne = "YOU DON'T HAVE TO YELL"; var stringTwo = (); // "you don't have to yell" var stringThree = (); // "you don't have to yell"
Pattern matching
In a string matching pattern, two methods can be used, and the basic working methods are the same.
A string match() method is called and passed through a regular expression:
var myString = "How much wood could a wood chuck chuck"; var myPattern = /.ood/; var myResult = (myPattern); // ["wood"] var patternLocation = ; // 9 var originalString = // "How much wood could a wood chuck chuck"
The exec() method is called a regular expression object and passed through a string:
var myString = "How much wood could a wood chuck chuck"; var myPattern = /.huck/; var myResult = (myString); // ["chuck"] var patternLocation = ; // 27 var originalString = // "How much wood could a wood chuck chuck"
For both methods, only the first match occurs is returned. If no match is found, a null value will be returned.
You can also use the search() method, which takes a regular expression as the only parameter and returns where the pattern first appears:
var myString = "Assume"; var patternLocation = (/ume/); // 3
If no match is found, the method returns "-1".
Comparison of the sort order of two strings
You can compare two strings and see which letter is first used to use localeCompare, there are three possible return values:
var myString = "chicken"; var myStringTwo = "egg"; var whichCameFirst = (myStringTwo); // -1 (except Chrome, which returns -2) whichCameFirst = ("chicken"); // 0 whichCameFirst = ("apple"); // 1 (Chrome returns 2) (target)//formula
As shown in the figure above, if stringObject is less than target, localeCompare() returns a number less than 0. If stringObject is greater than target, the method returns a number greater than 0. If the two strings are equal, or there is no difference according to local sorting rules, the method returns 0.
Since the browser can return any negative or positive results before and after any negative or positive numbers, it is best to use if ( result < 0 ) instead of if ( result === -1 ), the latter will not work in the Chrome browser.
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!