codecamp

Node.js 字符串

JavaScript中的字符串是Unicode字符序列。我们使用长度为1的字符串来表示字符。字符串可以用单引号或双引号括起来,它们在功能上等同。

要在单引号字符串中包含单引号,我们可以使用\',而对于双引号字符串中的双引号,我们可以使用\":

console.log('Javascript\'s new feature.')
console.log("\"Hey, new feature!\", he said.")

上面的代码生成以下结果。

字符串表达

例子

要在JavaScript中获取字符串的长度,只需使用length属性:

var x = "cat";
console.log(x.length);

上面的代码生成以下结果。

length属性结果

要将两个字符串添加到一起,可以使用+运算符:

var s = "CSS" + " is " + "cool";
console.log(s);

上面的代码生成以下结果。

+运算符结果

Javascript的+ 可以将它们转换为最佳值:

var distance = 25;
var s = "This is a test: " + distance + ".";
console.log(s);

上面的代码生成以下结果。



Node.js 布尔值
Node.js 字符串函数
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }