6

Is it possible to insert line breaks into a character string like this that automatically adjust so it don't split up words?

 nif <- as.character(c("I am a string", "So am I", 
     "I am also a string but way to long"))

I found this code in a post but it splits up words and also add a line break after each string which I would like to avoid

  gsub('(.{1,20})', '\\1\n',nif)

The output I am going for is this:

 "I am a string"    "So am I"     "I am also a string but \n way to long" 
Community
  • 1
  • 1
Einnor
  • 239
  • 1
  • 3
  • 12

2 Answers2

20

You can also use strwrap.

strwrap(nif, 20)
# [1] "I am a string"      "So am I"            "I am also a string"
# [4] "but way to long"   
sapply( strwrap(nif, 20, simplify=FALSE), paste, collapse="\n" )
# [1] "I am a string"                       "So am I"                            
# [3] "I am also a string\nbut way to long"
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • 1
    [answering a deleted comment] The UTF-8 encoding is apparently preserved if *all* the elements of the vector are in UTF-8 -- but if one contains only ascii characters, its encoding is always "unknown", and the encoding of the others is discarded: `x <- c("a","あ"); Encoding(x); Encoding(strwrap(x))`... You can fix the encoding afterwards with `Encoding(result) <- "UTF-8"`. – Vincent Zoonekynd May 19 '13 at 22:34
  • There seems to be a bug in Ecoding(x) <- Value It converts only some of my strings to UTF-8. How to repport? – Einnor May 21 '13 at 12:58
  • That is a documented behaviour: if the string only contains ascii characters, its encoding will always be "unknown" (and you cannot change it), because it is the same regardless of the encoding. If you think you have found a bug (`Encoding` may be fine, but `strwrap` should probably not discard the encoding in that way), you can discuss it on [R-help](https://stat.ethz.ch/mailman/listinfo/r-help). – Vincent Zoonekynd May 21 '13 at 13:24
6

You can find the first space after some number of letters and replace it with \n

For example something like this

 gsub('(.{18})\\s(.*)', '\\1\n\\2',nif)

[1] "I am a string"                       "So am I"                            
    "I am also a string\nbut way to long"
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thanks it works perfect. I got to learn regex but have yet to find a good source og tutorial. – Einnor May 19 '13 at 11:52
  • @Einnor there are many resources on the subject throght the net, just google it. somethink like 'R regerx tutorial'... – agstudy May 19 '13 at 11:55
  • Thanks. One more thing though. is it possible to add to the cide you just provided, that it should split multiple times in a string that are very very long? – Einnor May 19 '13 at 12:02