Josh
Josh

Reputation: 1278

why does `cat` append a space when displaying multiple strings?

Let's say I have the following vector:

output <- c("foo\n", "bar\n")

If I want to display this via cat, I get a weird indentation:

> cat(output)
foo
 bar

Why is there a space before "bar"? I can get around it with paste:

> cat(paste(output, collapse = ""))
foo
bar

but I don't understand why cat is behaving in this fashion in the first place? Is this a bug, or am I missing something about how cat behaves?

Upvotes: 0

Views: 61

Answers (1)

hrvg
hrvg

Reputation: 506

cat has a sep argument.

From ?cat:

sep: a character vector of strings to append after each element.

sep defaults to " ".

Hence:

output = c("foo\n", "bar\n")
cat(output, sep = "")
foo
bar

Upvotes: 3

Related Questions