R语言 打印字符串中新行
在这篇文章中,我们将讨论如何使用R编程语言在字符串中打印一个新行。
方法1:使用cat()函数
在这个方法中,需要调用cat()函数,它在这里负责打印文本和文字。
语法: cat(text/file = “”, sep = ” “, fill = FALSE, labels = NULL, append = FALSE)
参数
- Text/file:你想打印的文本或文件。
- sep:分隔符
- fill:如果fill=TRUE,如果页面被填满,将打印一个新行。
- labels: 标签,如果有的话
# storing strings in variables
string1 <- "GEEKS"
string2 <- "FOR"
string3 <- "GEEKS"
# passing variable in cat() without new
# line serperator
cat(string1,string2,string3)
# passing a string using \n to split
cat("GEEKS \nFOR \nGEEKS")
# passing variables using \n
cat(string1,"\n",string2,"\n",string3)
输出
GEEKS FOR GEEKS
GEEKS
FOR
GEEKS
GEEKS
FOR
GEEKS
方法2:使用writeLines()函数
在这个方法中,用户必须通过调用writeline()函数并在R编程语言中传递所需的参数来使用它。
writeLines() 用于在一个连接中写行。
语法: writeLines(text, con = stdout(), sep = “\n”, useBytes = FALSE)
参数
- text:你想打印的文本
- con。一个连接对象或一个字符串。
- Sep:分隔符
- UseBytes:True/False
# declaring variable
x <- "GEEKS \n FOR \n GEEKS"
# print x variable
writeLines(x)
# printing a string with \n new line
writeLines("GEEKS \nFOR \nGEEKS")
输出
GEEKS
FOR
GEEKS
GEEKS
FOR
GEEKS