gpt4 book ai didi

r - 如何避免 R sprintf ("very very long string with line break") 中的换行符?

转载 作者:行者123 更新时间:2023-12-01 19:14:17 33 4
gpt4 key购买 nike

我在 sprintf() 中有一个很长的字符串。它太长了,以至于打破它会很有用(可读性)(但仅限于源代码,而不是输出)。但是每当我中断长字符串时,它都会引入 \n ,因此输出也有一个换行符。如何打断源代码中的字符串,使其在输出中不被打断?

最佳答案

也许像下面这样的东西会很有用(尽管在不知道您的输入字符串实际是什么样子或您打算如何使用它们的情况下很难判断)。

Fmt <- c(" %s is %d feet tall.\n", 
"%s likes chocolate.\n",
"%s eats %d bars of chocolate",
"every night after dinner.")

sprintf(paste(Fmt, collapse = " "), "Sven", 7, "Sven", "He", 3)
# [1] " Sven is 7 feet tall.\n Sven likes chocolate.\n He eats 3 bars of chocolate every night after dinner."
cat(.Last.value)
# Sven is 7 feet tall.
# Sven likes chocolate.
# He eats 3 bars of chocolate every night after dinner.

关于r - 如何避免 R sprintf ("very very long string with line break") 中的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018473/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com