js study concluded (2)

This part describes the string type commonly used method

 

How to use these methods? Described by the following code:

There are a = '15693';

console.log(a.indexOf(5));

That variable name. Method ()

 

1.indexOf (), to determine whether there is a fixed value, if present, it returns the index position, or -1 does not exist, such as:

There are a = '15693';

console.log(a.indexOf(5));

We can get the return value of 1, why 1? We can see a string from left to right in the second position, and the index is zero-based, so the index is 1 second position

 

2.replace (), to replace the string value, such as

There are a = '15693';

var b = a.replace ( '156', 'I');

console.log(b);

This time we can see the value of my 93 print, that '156' is replaced by 'I' of the

 

3.split (), used to partition the string values, such as:

There are a = '15693';

var b = a.split ( '6');

console.log(b);

This time we can see the value is printed into an array [ '15', '96'], i.e., 6 to the split point, the split of the string to, the general methods we have used an array of strings turn

 

4.search (), used to find a character, such as

There are a = '15693';

console.log(a.indexOf(5));

And return to the position as indexOf 5 is located, but the difference is and indexOf, search by regular matches, but is based on string matching indexOf

 

5.slice (), used to extract a character position, it returns a new string, such as:

There are a = '15693';

var b = a.slice(0, 2);

console.log(b);

Returns the value of '15', and why? slice has two parameters, start and end positions, slice will be extracted based on these two positions, but does not include the value of the extracted content end position.

If the end position is negative, then extracted until the end of a character as the start position, for example:

a.slice var c = (0, -1);

This time value of c is '1569'

 

6.substr (), used to intercept characters between the two positions, such as:

There are a = '15693';

var b = a.substr(0, 2);

console.log(b);

The return value is '15'

 

7.substring (), used to intercept characters between the two positions, such as:

There are a = '15693';

var b = a.substring(0, 2);

console.log(b);

The return value is '15'

 

8.match (), find matching using regular characters, such as:

There are a = '15693';

var b = a.match(/15/);

console.log(b);

At this time, the string 15, it returns an array, the array is the first value 15. If none is found, then it will return null

 

9.toUpperCase () and toLowerCase (), the case of letters in the string conversion

 

10.trim (), both ends of the string to remove whitespace

 

11.charAt (), to return to a position of a character, such as:

There are a = '15693';

var b = a.charAt (2);

console.log(b);

The value returned 6

 

Guess you like

Origin www.cnblogs.com/woywan/p/12626451.html