Javascript String
Creating strings
let str1 = 'abc';
let str2 = "abc";
escaping quotes in string
let str3 = 'I\'ve got'; # by putting a backslash just before the character.
Escape sequence 转义序列
let str1 = 'abc';
let str2 = "abc";
escaping quotes in string
let str3 = 'I\'ve got'; # by putting a backslash just before the character.
Escape sequence 转义序列
\0 \' \" \\ \n \r \v \t \b \f \uXXXX : \u000A = \n, \u0021 = ! \u{X}...\u{XXXX} 0000-FFFF \xXX 00 -FF
joining strings together
连接字符串, use plus + operator, you can join as many as you like, as long as you include a + between each pair.
const button = document.querySelector('button'); button.onclick = function() { let name = prompt('What is your name?'); alert('Hello ' + name + ', nice to see you!'); }
用字符串表示为一个数字不行,但是用数字表示一个字符串可以。
'Front ' + 242;
let myString = '123'; let myNum = Number(myString); typeof myNum;
let myNum2 = 123; let myString2 = myNum2.toString(); typeof myString2;
~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTML provides structure and meaning to our text.
CSS allows us to precisely style it.
JavaScript contains a number of features for manipulating strings, creating custom welcome messages and prompts, showing the right text labels when needed, sorting terms into the desired order, and much more.
9:00
Template literals 模板文本
把字符串变成模板,你需要 replace the quote marks with backtick characters (``)
把字符串变成模板,你需要 replace the quote marks with backtick characters (``)
let song = 'Fight the Youth';
Would be turned into a template literal like so:
song = `Fight the Youth`;
If we want to concatenate strings, or include expression results inside them, traditional strings can be fiddly to write:
let score = 9; let highestScore = 10; let output = 'I like the song "' + song + '". I gave it a score of ' + (score/highestScore * 100) + '%.';
Template literals simplify this enormously:模板文本极大地简化了这一点:
output = `I like the song "${ song }". I gave it a score of ${ score/highestScore * 100 }%.`;
When you want to include a variable or expression inside the string, you include it inside a ${ } construct, which is called a placeholder.
let examScore = 45; let examHighestScore = 70; examReport = `You scored ${ examScore }/${ examHighestScore } (${ Math.round(examScore/examHighestScore*100) }%). ${ examScore >= 49 ? 'Well done, you passed!' : 'Bad luck, you didn\'t pass this time.' }`;
Template literals respect the line breaks in the source code, so newline characters are no longer needed. This would achieve the same result:
output = `I like the song "${ song }". I gave it a score of ${ score/highestScore * 100 }%.`;
let quote = 'I do not like green eggs and ham. I do not like them, Sam-I-Am.'; let substring = 'green eggs and ham'; // Add your code here let quoteLength = quote.length; let index = quote.indexOf(substring); let revisedQuote = `I do not like ${substring}.`; let revisedQuote = `${quote.substr(0,index)}${substring}.`; // Don't edit the code below here! section.innerHTML = ' '; let para1 = document.createElement('p'); para1.textContent = `The quote is ${ quoteLength } characters long.`; let para2 = document.createElement('p'); para2.textContent = revisedQuote; section.appendChild(para1); section.appendChild(para2);
indexOf('hello'); replace('a','b'); toLowerCase();substr(0,1)
(5 ** 2 + 8 ** 2) ** 0.5
阅读量: 629
发布于:
修改于:
发布于:
修改于: