One of the built-in features of JavaScript is the string connection, which, if you connect two numbers with ' + ', represents the two number added. However, if used with a string, the second word multibyte after the first character.
var num=1+2;console.log (num), var msg= ' hello ' + ' world '; Console.log (msg);
for strings, there are many other useful properties besides the length property, such as:
var str= ' Hello,world '; Console.log (str.length); Console.log (Str.charat (0)); The first character of Console.log (Str.charat (str.length-1)); The last character,//substring (Starti,endi), intercepts the beginning startistartii of the string, ends with the Endi, and contains no tail, without//If the second parameter does not, it is truncated to the last one by default. Console.log (str.substring (1,4)); Console.log (str.substring (1));//usage Ibid, negative, is the countdown to start counting, a parameter meaning is the countdown. Console.log (Str.slice (1,4)); Console.log (Str.slice (-3));//The position of the first occurrence of the character ' L ' Console.log (Str.indexof (' l '));//character ' The last occurrence of the position Console.log (Str.lastindexof (' l ')),//after the position Subscript 3, the first occurrence of the position Console.log (Str.indexof (' l ', 3));//with ', ' Split into String Console.log (Str.split (', '));//Convert lowercase h in str to uppercase Hconsole.log (str.replace (' H ', ' h '));//convert String to uppercase Console.log ( Str.touppercase ());
Tip: In JavaScript, the string itself is fixed, and the above method returns a new string value without affecting the value of the STR itself
It is worth noting that in ES6, there are a number of new methods added to the string, such as:
var s = ' Hello world! '; /Returns a Boolean value that indicates whether the parameter string is in the header of the source string Console.log (S.startswith (' Hello '))//True//endswith (): Returns a Boolean value, Indicates whether the parameter string is console.log at the end of the source string (S.endswith ('! '))//true//includes () returns a Boolean value indicating whether the parameter string was found Console.log (s.includes (' o '))/ /True
The three methods above support the second parameter, indicating where to start the search
var s = ' Hello world! '; Console.log (S.startswith (' World ', 6))//Trueconsole.log (S.endswith (' Hello ', 5))//Trueconsole.log (S.includes (' Hello ', 6))//False
Tip: The behavior of EndsWith differs from the other two methods. It is for the first n characters, while the other two methods are for the nth position until the end of a string.
The Repeat method returns a new string indicating that the original string is repeated n times.
Console.log (' x '. Repeat (3))//"XXX" console.log (' Hello '. Repeat (2))//"Hellohello" Console.log (' Na '. Repeat (0))//""
If the parameter is a decimal, it will be rounded.
Console.log (' Na '. Repeat (2.9))//"Nana"///If the repeat parameter is negative or infinity, an error will be said. Console.log (' Na '. Repeat (Infinity))//RangeErrorconsole.log (' Na '. Repeat ( -1))//Rangeerror However, if the parameter is a decimal between 0 and-1, the equivalent is 0, This is because the rounding operation is performed first. Decimals between 0 and 1, rounding equals -0,repeat as 0.
Console.log (' Na '. Repeat (-0.9))//""//Parameter NaN equals 0console.log (' Na '. Repeat (NaN))//"//If the repeat parameter is a string, will be converted to digital console.log (' na '. Repeat (' na '))//"" Console.log (' Na '. Repeat (' 3 '))//"Nanana"
ES2017 introduces the function of string complement length. If a string does not have a specified length, it is complete at the head or tail. Padstart () for head completion, padend () for tail completion
Padstart and Padend accept a total of two parameters, the first parameter is used to specify the minimum length of the string, and the second argument is the string to complement. Console.log (' x '. Padstart (5, ' AB '))//' ABABX ' Console.log (' x '. Padstart (4, ' AB '))/' Abax ' Console.log (' x '. Padend (5, ' AB '))//' Xabab ' Console.log (' x '. Padend (4, ' AB '))//' Xaba '///If the length of the original string is equal to or greater than the specified minimum length, the original string is returned. Console.log (' xxx '. Padstart (2, ' AB '))/' xxx ' console.log (' xxx ' padend (2, ' AB '))//' xxx '//if used to complement the string with the original string, If the sum of the two lengths exceeds the specified minimum length, the complement string that exceeds the number of digits is truncated. Consoe.log (' abc '. Padstart (' 0123456789 '))//' 0123456ABC '///If you omit the second argument, the default is to use a space to complement the full length. The common use of Console.log (' X '. Padstart (4))//' X ' Console.log (' x '. Padend (4))//' X '//padstart is to specify the number of digits for the numeric complement. The following code generates a 10-bit numeric string. Console.log (' 1 '. Padstart (0 '))//"0000000001" Console.log (' 0 ')/' padstart ')//' 0000000012 ' Console.log (' 123456 '. Padstart (10, ' 0 '))//"0000123456"//Another use is to prompt the string format. Console.log (' Padstart ', ' yyyy-mm-dd ')//"yyyy-mm-12" Console.log (' 09-12 ') padstart (' yyyy-mm-dd ')//" Yyyy-09-12 "
Can enter the group of communication ~web front-end interactive communication group 434623999
Summary of common usage methods for string-related strings in JavaScript