JS Operation string
From:https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/ 001434499203693072018f8878842a9b0011e3ff4e38b6b000
Template string
To concatenate multiple strings, you can connect them with the + sign:
var name = ' Xiao Ming ';
var age = 20;
var message = ' Hello, ' + name + ', you are this year ' + Age + ' years old! ';
alert (message);
If there are many variables that need to be connected, using the + sign is more troublesome. ES6 adds a template string that represents the same method as the multiline string above, but it automatically replaces the variables in the string:
var name = ' Xiao Ming ';
var age = 20;
Alert (' Hello, ${name}, you ${age} year old! ');
var name = ' Xiao Ming ';
var age = 20;
var message = ' Hello, ${name}, you ${age} year old! ';
alert (message);
Exercise: Test whether your browser supports ES6 template strings, and if not, change the template string to a normal string of + connections:
If the browser supports template strings, the variables inside the string will be replaced:
var s = ' Hello, world! ';
S.length; 13
var s = ' Hello, world! ';
S[0]; H
S[6]; ‘ ‘
S[7]; ' W '
S[12]; ‘!‘
S[13]; Undefined out of range index will not error, but return undefined
JavaScript provides some common methods for strings, note that calling these methods does not alter the contents of the original string, but instead returns a new string:
toUpperCase
toUpperCase () turns a string all uppercase:
var s = ' Hello ';
S.touppercase (); Return ' HELLO '
toLowerCase
toLowerCase () turns a string all lowercase:
var s = ' Hello ';
var lower = S.tolowercase (); Returns ' Hello ' and assigns a value to the variable lower
Lower ' Hello '
IndexOf
IndexOf () searches for the location where the specified string appears:
var s = ' Hello, world ';
S.indexof (' World '); Returns 7
S.indexof (' World '); The specified substring was not found, returned-1
Substring
SUBSTRING () returns the substring of the specified index interval:
var s = ' Hello, world '
S.substring (0, 5); Starting from index 0 to 5 (excluding 5), return ' Hello '
S.substring (7); Starting at index 7 to the end, return ' world '
JS Operation string