0094 String String

May be any text string in quotation marks, the syntax is double quotation marks "" and single quotation marks ''

var strMsg = "我爱北京天安门~";  // 使用双引号表示字符串
var strMsg2 = '我爱吃猪蹄~';    // 使用单引号表示字符串
// 常见错误
var strMsg3 = 我爱大肘子;       // 报错,没使用引号,会被认为是js代码,但js没有这些语法

Because HTML tags inside the property using double quotes, JS Here we recommend the use of single quotes .


1. String nested quotes

JS may be nested double quotes single quotes , or by double quotes nested single quotes ( outer double a single, dual single outer )

var strMsg = '我是"高帅富"程序猿';   // 可以用''包含""
var strMsg2 = "我是'高帅富'程序猿";  // 也可以用"" 包含''
//  常见错误
var badQuotes = 'What on earth?"; // 报错,不能 单双引号搭配

2. The escape character string

Inside an HTML-like special characters, special characters strings have, we call escape character.

We are beginning to escape, and the common escape follows:

Escapes explain
\n Newline, n being meant newline
 \ Slash \
' ' apostrophe
" "Double quotes
\t tab indentation
\b Space, b is the meaning of blank
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        alert('酷热难耐,火辣的太阳底下,我挺拔的身姿,成为了最为独特的风景。\n我审视四周,这里,是我的舞台,我就是天地间的王者。\n这一刻,我豪气冲天,终于大喊一声:"收破烂啦~"');
    </script>
</head>

<body>

</body>

</html>

3. string length

String is composed of several characters, the number of the characters is the length of the string. The length of the entire string may be acquired by the attribute string length.

var strMsg = "我是帅气多金的程序猿!";
alert(strMsg.length); // 显示 11

4. The string concatenation

  • New string may be used between a plurality of strings + splicing, splicing it to a string of any type = + after splicing

  • Any type string before splicing will turn into a string added, and then spliced ​​into a new string

    //1.1 字符串 "相加"
    alert('hello' + ' ' + 'world'); // hello world
    //1.2 数值字符串 "相加"
    alert('100' + '100'); // 100100
    //1.3 数值字符串 + 数值
    alert('11' + 12);     // 1112 
    • + No summary formulas: adding value, bridged characters

5. Strengthen string concatenation

console.log('pink老师' + 18);        // 只要有字符就会相连 
var age = 18;
console.log('pink老师age岁啦');      // 这样不行哦
console.log('pink老师' + age);         // pink老师18
console.log('pink老师' + age + '岁啦'); // pink老师18岁啦
  • Will often string variables and splicing, the variable can easily modify the value inside
  • Variables can not add quotes, because the variable becomes a string of quotes
  • If both variables have string concatenation, formulas "primer cited jerk" delete numbers, plus write intermediate variables

Guess you like

Origin www.cnblogs.com/jianjie/p/12128335.html