Common operation methods of JavaScript strings and arrays

JavaScript basic II

1. String

1.1 single line string

JavaScript strings are represented by characters enclosed by '' or ''

"abc"					    // Double quotation mark
'abc' 				    	// Single quotation mark

If you want to output '' or ', you need to add escape characters

Escape characters \ many characters can be escaped

\n							//Indicates a line break
\t							//Represents a tab
\ 							//Escape itself
\\							//Character represented by\

1.2 multiline string

ES6 standard adds a new representation method of multi line string, which is expressed in back quotation marks:

`first line
 Second line
 Third line
 ... ...`

1.3 template string

ES6 adds a new template string. The representation method is the same as the multi line string above. It will automatically replace the variables in the string

let name = 'Xiao Ming';
let age = 20;
let message = `Hello, ${name}, You this year ${age}Years old!`;
console.log(message);

1.4 common operations of string

Gets the length of the string

let s = 'Hello, world!';



s.length; 					// 13

Gets the character at a specified position in a string

var s = 'Hello, world!';

s[6]; 						// ' '
s[7]; 						// 'w'
s[13]; 						// Undefined indexes that exceed the range will not report an error, but will always return undefined

1.5 common methods of string

1.5.1 toUpperCase

toUpperCase() capitalizes a string:

var s = 'Hello';
s.toUpperCase(); // Return to 'HELLO'
1.5.2 toLowerCase

toLowerCase() changes all strings to lowercase:

var s = 'Hello';
var lower = s.toLowerCase(); // Return 'hello' and assign it to the variable lower
lower; // 'hello'
1.5.3 indexOf

indexOf() searches where the specified string appears:

var s = 'hello, world';
s.indexOf('world'); // Return 7
s.indexOf('World'); // If the specified substring is not found, - 1 is returned
1.5.4 substring

substring() returns the substring of the specified index interval:

var s = 'hello, world'
s.substring(0, 5); // From index 0 to 5 (excluding 5), return 'hello'
s.substring(7); // From index 7 to the end, return 'world'

2. Array

2.1 length of array

The elements of the array can be accessed by index. Note that the starting value of the index is 0:

var arr = [1, 2, 3.14, 'Hello', null, true];
arr[0]; // Returns the element with index 0, i.e. 1
arr[5]; // Returns the element with index 5, that is, true
arr[6]; // Index out of range, return undefined

To obtain the length of the Array, directly access the length attribute:

var arr = [1, 2, 3.14, 'Hello', null, true];
arr.length; // 6

Please note that directly assigning a new value to the length of the Array will change the size of the Array:

let arr = [1, 2, 3];
console.log(arr);								//[1, 2, 3]
console.log(arr.length);						//3

arr.length = 6;
arr[5] = 4;										//Modify the value of the sixth element

console.log(arr);								//[1, 2, 3, undefined, undefined, 4]
console.log(arr.length);						//6

arr[0] = 10;									//The array can directly modify the values of the corresponding elements in the following table
console.log(arr);								//[10, 2, 3, undefined, undefined, 4]

2.2 indexOf

Similar to String, Array can also search the location of a specified element through indexOf():

let arr = [1, 2, 3, 4, '4', 'hello', '!'];
      console.log(arr.length);							//7
      console.log(arr.indexOf(1));						//0
      console.log(arr.indexOf("hello"));                //5
      console.log(arr.indexOf("4"));					//4
      console.log(arr.indexOf("!"));					//6
      console.log(arr.indexOf("5"));					//-1

Note that the number 30 and the string '30' are different elements.

2.3 slice

slice() is the substring() version of the corresponding String. It intercepts some elements of the Array and returns a new Array:

var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
arr.slice(0, 3); // From index 0 to index 3, but excluding index 3: ['A', 'B', 'C']
arr.slice(3); // From index 3 to end: ['d ',' e ',' f ',' g ']

Note that the start and end parameters of slice() include the start index and not the end index.

If slice() is not passed any parameters, it intercepts all elements from beginning to end. Using this, we can easily copy an Array:

var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
var aCopy = arr.slice();
aCopy; // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
aCopy === arr; // false

2.4 push and pop

push() adds several elements to the end of the Array, and pop() deletes the last element of the Array:

var arr = [1, 2];
arr.push('A', 'B'); // Return the new length of Array: 4
arr; // [1, 2, 'A', 'B']
arr.pop(); // pop() returns' B '
arr; // [1, 2, 'A']
arr.pop(); arr.pop(); arr.pop(); // pop 3 times continuously
arr; // []
arr.pop(); // If the empty array continues, pop will not report an error, but return undefined
arr; // []

2.5 unshift and shift

If you want to add several elements to the head of the Array, use the unshift() method. The shift() method deletes the first element of the Array:

var arr = [1, 2];
arr.unshift('A', 'B'); // Return the new length of Array: 4
arr; // ['A', 'B', 1, 2]
arr.shift(); // 'A'
arr; // ['B', 1, 2]
arr.shift(); arr.shift(); arr.shift(); // 3 consecutive shifts
arr; // []
arr.shift(); // If the empty array continues to shift, no error will be reported, but undefined will be returned
arr; // []

2.6 sort

sort() can sort the current Array. It will directly modify the element position of the current Array. When called directly, it will be sorted according to the default order:

var arr = ['B', 'C', 'A'];
arr.sort();
arr; // ['A', 'B', 'C']

Can we sort according to the order we specify? Absolutely. We'll talk about it later in the function.

2.7 reverse

reverse() calls the elements of the entire Array, that is, reverse:

var arr = ['one', 'two', 'three'];
arr.reverse(); 
arr; // ['three', 'two', 'one']

2.8 splice

The splice() method is a "universal method" to modify the Array. It can delete several elements from the specified index, and then add several elements from this position:

var arr = ['Microsoft', 'Apple', 'Yahoo', 'AOL', 'Excite', 'Oracle'];
// Delete three elements from index 2, and then add two more elements:
arr.splice(2, 3, 'Google', 'Facebook'); // Return deleted elements ['yahoo ',' AOL ',' excite ']
arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
// Delete only, do not add:
arr.splice(2, 2); // ['Google', 'Facebook']
arr; // ['Microsoft', 'Apple', 'Oracle']
// Add only, not delete:
arr.splice(2, 0, 'Google', 'Facebook'); // Returned [], because no element was deleted
arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']

2.9 concat

The concat() method connects the current Array with another Array and returns a new Array:

var arr = ['A', 'B', 'C'];
var added = arr.concat([1, 2, 3]);
added; // ['A', 'B', 'C', 1, 2, 3]
arr; // ['A', 'B', 'C']

Note that the concat() method does not modify the current Array, but returns a new Array.

In fact, the concat() method can receive any element and Array, automatically disassemble the Array, and then add them all to the new Array:

var arr = ['A', 'B', 'C'];
arr.concat(1, 2, [3, 4]); // ['A', 'B', 'C', 1, 2, 3, 4]

2.10 join

The join() method is a very practical method. It connects each element of the current Array with the specified string, and then returns the connected string:

var arr = ['A', 'B', 'C', 1, 2, 3];
arr.join('-'); // 'A-B-C-1-2-3'

If the element of Array is not a string, it will be automatically converted to a string and then connected.

The above is basic II in JavaScript, crab support~~~

It is recommended to change the blogger. Part of the content is extracted from here Liao Xuefeng

Keywords: Java Javascript Front-end string

Added by asphpguy on Wed, 22 Dec 2021 23:07:39 +0200