JavaScript 字符串String
目录
字符串表示
字符串拼接
字符串换行
字符串长度
字符串循环
字符串方法
字符串案例
字符串表示
使用单或者双引号包裹的字符
var name = "zsh" var name = 'zsh'
字符串拼接
+ 把需要连接的字符串连接起来。
var a = "are" var b = "You" + a + "good" console.log(b) // Youaregood
`` 把字符串拼接起来
var a = "are"
var b = `You ${a} good`
console.log(b) // You are good
数组的jion()方法连接字符串
var a = ["hello", "world"]
var b = a.join("_")
console.log(b) // hello_world
concat 拼接字符串
var a = "hello"
var b = a.concat(" world")
console.log(b) // hello world
字符串换行
最佳换行的位置是某个运算符之后。
var a = "llllllllllllllllll" console.log(a) // lllllllll
字符串中换行
var a = "hello \ world" console.log(a) // hello world
note:
\ 方法不是JavaScript标准。
某些浏览器也不允许 \ 字符串之后的空格。
对长字符串换行的最佳做法是使用字符串加法。列如:
var a = "hello " + "world" console.log(a) // hello world
字符串长度
length 字符串内置属性,返回字符串长度
var a = "hello " console.log(a.length) // 6

浙公网安备 33010602011771号