JavaScript字符串介绍

In JavaScript, a string is any text between single or double quotes, including letters, numbers, and Unicode characters:

在JavaScript中, 字符串是单引号或双引号之间的任何文本,包括字母,数字和Unicode字符:

"This is a string"
'This is also a string'
"Forever 21, too, is a string"

Single quotes can be included in a string if the string itself is surrounded by double quotes, and vice-versa.

如果字符串本身被双引号引起来,则可以在字符串中包含单引号,反之亦然。

'This is "okay"'

While it doesn’t matter which option you choose, settle on one quote convention for strings and stick to it. This code style should be used by other developers in your team, together with other agreed-upon conventions, to gain consistency, avoid confusion, and reduce bugs.

尽管选择哪个选项都无关紧要, 但只需对字符串使用一个引号约定并坚持使用即可 。 您团队中的其他开发人员应将这种代码样式与其他已达成共识的约定一起使用,以获得一致性,避免混淆并减少错误。

Otherwise, quotes and other special characters inside a string should be escaped with a leading backwards slash character:

否则,引号和其他特殊字符的字符串里面应该逃脱领先的向后斜杠字符:

Escape StringRepresents
\'single quote
\"double quote
\\backslash
\nnew line
\rcarriage return
\ttab
转义字符串 代表
\' 单引号
\" 双引号
\\ 反斜杠
\n 新队
\r 回车
\t 标签

For example:

例如:

"This is \t Tab-delimited \t Text"

Since strings can contain Unicode characters, true “smart” quotes and apostrophe characters should be used where appropriate:

由于字符串可以包含Unicode字符,因此应在适当的地方使用真正的“智能”引号和撇号字符:

var quote = "“This is a quote.”";

索引字符串 (Indexing Strings)

Like arrays, the first character in a string is considered to be at position 0, the second one at position 1, and so on. Note that the length of a string includes every character in it, including spaces but excluding escape characters:

数组一样,字符串中的第一个字符位于位置0,第二个字符位于位置1,依此类推。 请注意,字符串的length包括其中的每个字符,包括空格,但不包括转义字符:

var nicchia = "Virginia Elisabetta Luisa Carlotta Antonietta Teresa Maria Oldoini";
nicchia.length
> 66

There are many functions and methods associated with strings, all of which I will cover in future articles.

有许多与字符串相关的函数和方法,我将在以后的文章中介绍所有这些函数和方法。

数字作为字符串 (Numbers as Strings)

Sometimes you’ll receive a number that JavaScript doesn’t appear to treat as one. A good example is an HTML5 form number input:

有时,您会收到一个似乎没有被JavaScript视为一个数字。 一个很好的例子是HTML5表单number输入

<label for="weight">Enter the weight of the package in kilos</label>
<input type="number" id="weight" min="1" max="10" value="0">

If we try to read the value using traditional JavaScript and add 1 to it:

如果我们尝试使用传统JavaScript读取值并将其添加1:

document.getElementById("weight");
weight.addEventListener("input", function() {
console.log(weight.value + 1);
})

… the result we get in the console when "1" is entered into the number field is not "2", but "11". This reflects the problem of concatenation: value treats the entry in the number field as a string, not a number. There are two ways of dealing with this:

…在数字字段中输入“ 1”时,我们在控制台中得到的结果不是“ 2”,而是“ 11”。 这反映了串联的问题: value将number字段中的条目视为字符串,而不是数字。 有两种处理方法:

  1. Use the correct method: weight.valueAsNumber, rather than weight.value, This will report the entry in the field as an actual number.

    使用正确的方法: weight.valueAsNumber ,而不是weight.value ,这会将字段中的条目报告为实际数字。

  2. Stick with the value method but convert the text entered into a true number using parseInt:

    坚持使用value方法,但使用parseInt将输入的文本转换为真数字:

weight = document.getElementById("weight");
weight.addEventListener("input", function() {
console.log(parseInt(weight.value, 10) + 1);
})

I’ll have more to say about parseInt in a future article.

在以后的文章中,我将对parseInt进行更多讨论。

将数字转换为字符串 (Converting Numbers Into Strings)

Sometimes you’ll want to do the reverse: convert a number into a string. If I have a variable named wonders:

有时,您需要做相反的事情:将数字转换为字符串。 如果我有一个名为wonders的变量:

var wonders = 7;

…I can convert it into a string by using .toString():

…我可以使用.toString()将其转换为字符串:

var wonderString = wonders.toString();

…or by adding an empty quote to it:

…或在其中添加空引号:

var wonderString = wonders + "";

Either works, although you may find the second method faster in loops (over millions of operations).

无论哪种方法都可以,尽管您可能会发现循环中的第二种方法更快(超过数百万次操作)。

翻译自: https://thenewcode.com/1022/JavaScript-Strings-Introduction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值