【问题标题】:Why can't I remove new line characters from being added with Node's exec function?为什么我不能从 Node 的 exec 函数中删除换行符?
【发布时间】:2012-03-30 18:21:45
【问题描述】:

我有以下使用 Node 执行 shell 命令:

function puts( error, stdout, stderr ) {
    stdout = sanitize( stdout ).rtrim("\n"); // remove new line
    sys.puts( stdout );
}

我使用这个库来尝试删除新行:

npm install validator

但是当我做这样的事情时:

exec( "ls -l", puts );

它在末尾放置一个换行符。

我使用的是Linux,所以新行应该只是\n 对吧?!

如何阻止新行发生?

非常感谢。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    您确定调用 sys.puts 不是添加换行符吗?试试 sys.print 看看你会得到什么。

    在其他新闻中,我认为 sys.puts(以及一般的 sys)在 node.js 中已经被弃用了一段时间。您可能应该使用 console.log 来处理这种事情。

    【讨论】:

    • 不.. 尝试了 console.log( ... ) 并且发生了同样的事情。这是我通常使用的:s。
    • sys.puts(现在是util.puts)和console.log都将追加换行符。正如 Cameron 建议的那样,尝试使用 {sys|util}.print。它不会附加换行符。
    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多