Summary of commonly used string usage methods in Javascript

Source: Internet
Author: User
This article mainly introduces the common usage of string in Javascript. It has good reference value. Next, let's take a look at the following small series. This article mainly introduces the common usage of string-related Javascript. It has good reference value. Let's take a look at it with the small Editor.

I was just reading the Javascript rhino book and saw the character string section. I usually don't have much work in this area. I want to sort it out, so I only need it from time to time.

One of the built-in functions of Javascript is string connection. If two numbers are connected with '+', the two numbers are added. However, if it is used as a string, it indicates that the second character is added after the first character.

var num=1+2;console.log(num);var msg='hello'+'world';console.log(msg);

For strings, apart from the length attribute, there are many other useful attributes, such:

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), start startistartii when the string is intercepted, end with endi, And the header does not contain the end, no // if the second parameter does not exist, the last parameter is intercepted by default. Console. log (str. substring (1, 4); console. log (str. substring (1); // The usage is the same as above. If it is a negative number, the countdown starts to calculate. The meaning of a parameter is the last few. Console. log (str. slice (1, 4); console. log (str. slice (-3); // the location where the character 'l' appears for the first time. log (str. indexOf ('l'); // The last time the character 'l' appears, console. log (str. lastIndexOf ('l'); // The console where the cursor first appears after position subscript 3. log (str. indexOf ('l', 3); // use ',' to split the string into console. log (str. split (','); // replace lowercase h in str with uppercase Hconsole. log (str. replace ('h', 'H'); // converts a string to an upper-case console. log (str. toUpperCase ());

Tip:In javascript, the string itself remains unchanged. The preceding method returns a new string value without affecting the str value.

It is worth noting that in ES6, many new methods are added to the string, such:

Var s = 'Hello world! '; // Returns a Boolean value, indicating whether the parameter string is in the console of the header of the source string. log (s. startsWith ('hello') // true // endsWith (): returns a Boolean value indicating whether the parameter string is in the console at the end of the source string. log (s. endsWith ('! ') // True // returns a Boolean value of pair des (), indicating whether the console. log (s. Pair des ('O') parameter is found. // true

The preceding three methods support the second parameter, indicating the start position of 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 is different from that of the other two methods. It is for the first n characters, while the other two methods are for starting from the nth position until the end of the string.
The repeat method returns a new string that repeats the original string 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 number, it is rounded up.

Console. log ('na'. repeat (2.9) // "nana" // If the repeat parameter is negative or Infinity, an error is returned. Console. log ('na'. repeat (Infinity) // RangeErrorconsole. log ('na'. repeat (-1) // RangeError

However, if the parameter is a decimal number between 0 and-1, it is equivalent to 0, because the integer operation is performed first. Decimal place between 0 and-1, which is equal to-0 after the integer is obtained, and the repeat value is equal to 0.

Console. log ('A '. repeat (-0.9) // "// The parameter NaN is equivalent to 0console. log ('A '. repeat (NaN) // "// If the repeat parameter is a string, it is first converted to a digital console. log ('A '. repeat ('na') // "console. log ('A '. repeat ('3') // "nanana"

ES2017 introduces the string completion length function. If the length of a string is not specified enough, it is completed in the header or tail. PadStart () is used for header completion, and padEnd () is used for Tail completion.

// Both padStart and padEnd parameters are accepted. The first parameter is used to specify the minimum length of the string, and the second parameter is used to complete the string. Console. log ('x '. padStart (5, 'AB') // 'ababx' console. log ('x '. padStart (4, 'AB') // 'abax' console. log ('x '. padEnd (5, 'AB') // 'xababab' 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 the string to be supplemented and the original string are used, the sum of the length of the two exceeds the specified minimum length, the string that exceeds the number of digits is truncated. When E. log ('abc'. padStart (10, '20140901') // '0123456abc' // if the second parameter is omitted, spaces are used to fill the length by default. Console. log ('x '. padStart (4) // 'X' console. log ('x '. padEnd (4) // 'X' // common use of padStart is to fill in the specified number of digits for the value. The following code generates a 10-bit numeric string. Console. log ('1 '. padStart (10, '0') // "0000000001" console. log ('12 '. padStart (10, '0') // "0000000012" console. log ('20140901 '. padStart (10, '0') // "0000123456" // another purpose is to prompt the string format. Console. log ('12 '. padStart (10, 'yyyy-MM-DD ') // "YYYY-MM-12" console. log ('09-12 '. padStart (10, 'yyyy-MM-DD ') // "YYYY-09-12"

The above is a summary of common string usage methods in Javascript. For more information, see other related articles in the first PHP community!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.