Summary of Common Usage Methods of String Correlation in Javascript

  • 2021-08-03 08:49:18
  • OfStack

I was just reading Javascript rhinoceros book, and I saw this section of string. I didn't come into contact with this aspect at ordinary times, so I thought about sorting it out in case I only need it from time to time.

One of the built-in features of JavaScript is string concatenation. If two numbers are concatenated with '+', it means that the two numbers are added. However, if it is used for strings, it means that the second character is added after the first character.


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

In addition to the length attribute, there are many other useful attributes for strings, such as:


var str='hello,world';
console.log(str.length);
console.log(str.charAt(0)); // No. 1 1 Characters  
console.log(str.charAt(str.length-1)); //  Finally 1 Characters  
//substring(starti,endi),  Interception   String   Start startistartii ,   To endi End, with head but no tail, no 
//  If the 2 There are no parameters,   It is intercepted to the last by default 1 A.   
console.log(str.substring(1,4));
console.log(str.substring(1));
// The usage is the same as above. When it is negative, it is the reciprocal. 1 The meaning of the parameters is the reciprocal. 
console.log(str.slice(1,4));
console.log(str.slice(-3));
// Character ' l' Position of first appearance  
console.log(str.indexOf('l'));
// Character ' l' Finally 1 The position of the second occurrence  
console.log(str.lastIndexOf('l'));
// Subscript at position 3 After that, the first place to appear  
console.log(str.indexOf('l',3));
// With ', ' Split into strings 
console.log(str.split(','));
//  Put str Lowercase in h Change to capitals H
console.log(str.replace('h','H'));
// Talking about converting strings to uppercase 
console.log(str.toUpperCase());

Tip: In javascript, the string itself is fixed, and the above methods will return a new string value, which will not affect the value of str itself

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


var s = 'Hello world!';
//  Returns a Boolean value indicating whether the parameter string is in the header of the source string 
console.log(s.startsWith('Hello')) // true
//endsWith() Returns a Boolean value indicating whether the parameter string is at the end of the source string 
console.log(s.endsWith('!')) // true
// includes() Returns a Boolean value indicating whether the parameter string was found 
console.log(s.includes('o')) // true

The above three methods all support the second parameter, which indicates the location where the search begins


var s = 'Hello world!';
console.log(s.startsWith('world', 6)) // true
console.log(s.endsWith('Hello', 5))// true
console.log(s.includes('Hello', 6)) // false

Hint: The behavior of endsWith is different from the other two methods. It targets the first n characters, while the other two methods target from the n position until the end of the 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 repeat Is a negative number or Infinity , will report an error. 
console.log('na'.repeat(Infinity))
// RangeError
console.log('na'.repeat(-1))
// RangeError

However, if the parameter is a decimal between 0 and-1, it is equivalent to 0, because rounding is performed first. The decimal between 0 and-1 equals-0 after rounding, and repeat is regarded as 0.


console.log('na'.repeat(-0.9)) // ""
// Parameter NaN Equivalent to 0
console.log('na'.repeat(NaN)) // "
// If repeat Is a string, it is converted to a number first 
console.log('na'.repeat('na')) // ""
console.log('na'.repeat('3')) // "nanana"

ES 2017 introduces the function of string completion length. If a string is not long enough for the specified length, it will be completed at the head or tail. padStart () for head completion, padEnd () for tail completion


//padStart And padEnd1 Accepts two parameters, the 1 Parameters are used to specify the minimum length of the string, the 2 Parameters are strings used to complete. 
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 the sum of the lengths of the completed string and the original string exceeds the specified minimum length, the completed string that exceeds the number of digits is truncated. 
consoe.log('abc'.padStart(10, '0123456789'))
// '0123456abc'
// If you omit the 2 Parameters, using space completion length by default. 
console.log('x'.padStart(4)) // ' x'
console.log('x'.padEnd(4)) // 'x '
//padStart Is to specify digits for numerical completion. The following code generates 10 A numeric string of bits. 
console.log('1'.padStart(10, '0') )// "0000000001"
console.log('12'.padStart(10, '0')) // "0000000012"
console.log('123456'.padStart(10, '0')) // "0000123456"
// Another 1 The purpose is to prompt 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"

Related articles: