在终端中运行JavaScript(使用rhino)时如何使用print()函数在一行中打印?

时间:2020-12-13 21:44:40

The function print() gives newline character at the end.

函数print()在最后给出了换行符。

following is the JS code

以下是JS代码

print("first");
Things =["car","building"]
for (var i = Things.length - 1; i >= 0; i--) 
{
    print(Things[i])
}

the Output I get for the code above is

我得到的上述代码的输出是

first
building
car

Whereas I intended to print like this

而我打算像这样打印

firstbuildingcar

How do I print them in a single line using print() function?

2 个解决方案

#1


0  

Create an array and join it using a blank delimiter:

创建一个数组并使用空白分隔符连接它:

print(['uno','dos','tres'].join("")); // outputs "unodostres"

You don't need to loop. You do need to create your array before joining it (whereas you're printing one standalone string that's not part of an array, and then looping over your array).

你不需要循环。您需要在加入数组之前创建数组(而您打印的是一个不属于数组的独立字符串,然后循环遍历数组)。

If you want to reverse the print order of your array-- which it looks like your loop is doing-- just reverse() it before you join():

如果你想颠倒你的数组的打印顺序 - 它看起来像你的循环正在做 - 只需在join()之前反转():

print(['uno','dos','tres'].reverse().join("")); // outputs "tresdosuno"

#2


0  

Things =["car","building"]
res ="first"
for (var i = Things.length - 1; i >= 0; i--) 
{
    res=res+Things[i]
}
print(res);

#1


0  

Create an array and join it using a blank delimiter:

创建一个数组并使用空白分隔符连接它:

print(['uno','dos','tres'].join("")); // outputs "unodostres"

You don't need to loop. You do need to create your array before joining it (whereas you're printing one standalone string that's not part of an array, and then looping over your array).

你不需要循环。您需要在加入数组之前创建数组(而您打印的是一个不属于数组的独立字符串,然后循环遍历数组)。

If you want to reverse the print order of your array-- which it looks like your loop is doing-- just reverse() it before you join():

如果你想颠倒你的数组的打印顺序 - 它看起来像你的循环正在做 - 只需在join()之前反转():

print(['uno','dos','tres'].reverse().join("")); // outputs "tresdosuno"

#2


0  

Things =["car","building"]
res ="first"
for (var i = Things.length - 1; i >= 0; i--) 
{
    res=res+Things[i]
}
print(res);