检测字符串是否被修剪
Detect a string if it is trimmed or not
`......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`.trim()
当我 trim 以上时,它给了我这个:
"......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................"
根据我的理解,trim 像 " helloworld "
一样从字符串的开头和结尾删除空格,trim 将是 "helloworld"
。现在我想知道我在上面定义的第一个例子。
首先,它是不是一个字符串,因为我在那里看到了反引号。如果我尝试给它引号,trim 将不起作用。我无法理解或看不出它是如何 trimmed 的。该示例来自 Eloquent JavaScript.
中的 platform game
Firs of all, is it even a string because I see backticks there. And if I try to give it quotes the trim won't work.
反引号允许您在多行上定义字符串。例如这样的字符串:
let str = `Hello
World`;
实际上是一串值"Hello\nWorld"
(两个词之间换行)。如果您用引号替换反引号并尝试像这样定义一个字符串:
let str = "Hello
World";
那么这不是一个有效的陈述。这就是它对你失败的原因。与trim函数无关。
这在 js 中无效:
"......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................"
这是(正如 Kunal Mukherjee 在评论中指出的,您必须使用支持多行的模板文字):
`......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`
要回答标题中的问题,检查字符串是否被修剪,您可以这样做:
function isTrimmed(str) {
return str == str.trim();
}
const foo = `......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`;
console.log(isTrimmed(foo));
console.log(isTrimmed(" Text with space "));
`......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`.trim()
当我 trim 以上时,它给了我这个:
"......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................"
根据我的理解,trim 像 " helloworld "
一样从字符串的开头和结尾删除空格,trim 将是 "helloworld"
。现在我想知道我在上面定义的第一个例子。
首先,它是不是一个字符串,因为我在那里看到了反引号。如果我尝试给它引号,trim 将不起作用。我无法理解或看不出它是如何 trimmed 的。该示例来自 Eloquent JavaScript.
中的 platform gameFirs of all, is it even a string because I see backticks there. And if I try to give it quotes the trim won't work.
反引号允许您在多行上定义字符串。例如这样的字符串:
let str = `Hello
World`;
实际上是一串值"Hello\nWorld"
(两个词之间换行)。如果您用引号替换反引号并尝试像这样定义一个字符串:
let str = "Hello
World";
那么这不是一个有效的陈述。这就是它对你失败的原因。与trim函数无关。
这在 js 中无效:
"......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................"
这是(正如 Kunal Mukherjee 在评论中指出的,您必须使用支持多行的模板文字):
`......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`
要回答标题中的问题,检查字符串是否被修剪,您可以这样做:
function isTrimmed(str) {
return str == str.trim();
}
const foo = `......................
..#................#..
..#..............=.#..
..#.........o.o....#..
..#.@......#####...#..
..#####............#..
......#++++++++++++#..
......##############..
......................`;
console.log(isTrimmed(foo));
console.log(isTrimmed(" Text with space "));