将单词包装成段落,使用 Knuth-Plass 算法最小化行的 "raggedness"(即行长度的变化)。
参数
- string
-
输入向量。或者是一个字符向量,或者是可强制转换为一个的东西。
- width
-
给出目标行宽度的正整数(以字符数为单位)。宽度小于或等于 1 将使每个单词独占一行。
- indent, exdent
-
一个非负整数,给出第一行 (
indent
) 和所有后续行 (exdent
) 的缩进。 - whitespace_only
-
一个布尔值。
-
如果
TRUE
(默认)仅在空白处发生换行。 -
如果
FALSE
,可以在任何非单词字符上中断(例如/
、-
)。
-
也可以看看
stringi::stri_wrap()
用于底层实现。
例子
thanks_path <- file.path(R.home("doc"), "THANKS")
thanks <- str_c(readLines(thanks_path), collapse = "\n")
thanks <- word(thanks, 1, 3, fixed("\n\n"))
cat(str_wrap(thanks), "\n")
#> R would not be what it is today without the invaluable help of these people
#> outside of the (former and current) R Core team, who contributed by donating
#> code, bug fixes and documentation: Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben Bolker, David Brahm,
#> G"oran Brostr"om, Patrick Burns, Vince Carey, Saikat DebRoy, Matt Dowle, Brian
#> D'Urso, Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian Fischmeister,
#> John Fox, Paul Gilbert, Yu Gong, Gabor Grothendieck, Frank E Harrell Jr, Peter
#> M. Haverty, Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger Koenker,
#> Philippe Lambert, Jan de Leeuw, Jim Lindsey, Patrick Lindsey, Catherine Loader,
#> Gordon Maclean, Arni Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#> Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert Palme, Roger D. Peng,
#> Jose' C. Pinheiro, Tony Plate, Anthony Rossini, Jonathan Rougier, Petr Savicky,
#> Guenther Sawitzki, Marc Schwartz, Arun Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry Therneau, Rolf Turner, Bill Venables,
#> Gregory R. Warnes, Andreas Weingessel, Morten Welinder, James Wettenhall, Simon
#> Wood, and Achim Zeileis. Others have written code that has been adopted by R and
#> is acknowledged in the code files, including
cat(str_wrap(thanks, width = 40), "\n")
#> R would not be what it is today without
#> the invaluable help of these people
#> outside of the (former and current) R
#> Core team, who contributed by donating
#> code, bug fixes and documentation:
#> Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson,
#> Roger Bivand, Ben Bolker, David Brahm,
#> G"oran Brostr"om, Patrick Burns, Vince
#> Carey, Saikat DebRoy, Matt Dowle,
#> Brian D'Urso, Lyndon Drake, Dirk
#> Eddelbuettel, Claus Ekstrom, Sebastian
#> Fischmeister, John Fox, Paul Gilbert,
#> Yu Gong, Gabor Grothendieck, Frank E
#> Harrell Jr, Peter M. Haverty, Torsten
#> Hothorn, Robert King, Kjetil Kjernsmo,
#> Roger Koenker, Philippe Lambert, Jan
#> de Leeuw, Jim Lindsey, Patrick Lindsey,
#> Catherine Loader, Gordon Maclean,
#> Arni Magnusson, John Maindonald,
#> David Meyer, Ei-ji Nakama, Jens
#> Oehlschl"agel, Steve Oncley, Richard
#> O'Keefe, Hubert Palme, Roger D. Peng,
#> Jose' C. Pinheiro, Tony Plate, Anthony
#> Rossini, Jonathan Rougier, Petr Savicky,
#> Guenther Sawitzki, Marc Schwartz, Arun
#> Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry
#> Therneau, Rolf Turner, Bill Venables,
#> Gregory R. Warnes, Andreas Weingessel,
#> Morten Welinder, James Wettenhall, Simon
#> Wood, and Achim Zeileis. Others have
#> written code that has been adopted by R
#> and is acknowledged in the code files,
#> including
cat(str_wrap(thanks, width = 60, indent = 2), "\n")
#> R would not be what it is today without the invaluable
#> help of these people outside of the (former and current)
#> R Core team, who contributed by donating code, bug fixes
#> and documentation: Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben
#> Bolker, David Brahm, G"oran Brostr"om, Patrick Burns,
#> Vince Carey, Saikat DebRoy, Matt Dowle, Brian D'Urso,
#> Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian
#> Fischmeister, John Fox, Paul Gilbert, Yu Gong, Gabor
#> Grothendieck, Frank E Harrell Jr, Peter M. Haverty,
#> Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger
#> Koenker, Philippe Lambert, Jan de Leeuw, Jim Lindsey,
#> Patrick Lindsey, Catherine Loader, Gordon Maclean, Arni
#> Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#> Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert
#> Palme, Roger D. Peng, Jose' C. Pinheiro, Tony Plate, Anthony
#> Rossini, Jonathan Rougier, Petr Savicky, Guenther Sawitzki,
#> Marc Schwartz, Arun Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry Therneau, Rolf Turner,
#> Bill Venables, Gregory R. Warnes, Andreas Weingessel, Morten
#> Welinder, James Wettenhall, Simon Wood, and Achim Zeileis.
#> Others have written code that has been adopted by R and is
#> acknowledged in the code files, including
cat(str_wrap(thanks, width = 60, exdent = 2), "\n")
#> R would not be what it is today without the invaluable help
#> of these people outside of the (former and current) R
#> Core team, who contributed by donating code, bug fixes
#> and documentation: Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben
#> Bolker, David Brahm, G"oran Brostr"om, Patrick Burns,
#> Vince Carey, Saikat DebRoy, Matt Dowle, Brian D'Urso,
#> Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian
#> Fischmeister, John Fox, Paul Gilbert, Yu Gong, Gabor
#> Grothendieck, Frank E Harrell Jr, Peter M. Haverty,
#> Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger
#> Koenker, Philippe Lambert, Jan de Leeuw, Jim Lindsey,
#> Patrick Lindsey, Catherine Loader, Gordon Maclean, Arni
#> Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#> Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert
#> Palme, Roger D. Peng, Jose' C. Pinheiro, Tony Plate,
#> Anthony Rossini, Jonathan Rougier, Petr Savicky, Guenther
#> Sawitzki, Marc Schwartz, Arun Srinivasan, Detlef Steuer,
#> Bill Simpson, Gordon Smyth, Adrian Trapletti, Terry
#> Therneau, Rolf Turner, Bill Venables, Gregory R. Warnes,
#> Andreas Weingessel, Morten Welinder, James Wettenhall,
#> Simon Wood, and Achim Zeileis. Others have written code
#> that has been adopted by R and is acknowledged in the code
#> files, including
cat(str_wrap(thanks, width = 0, exdent = 2), "\n")
#> R
#> would
#> not
#> be
#> what
#> it
#> is
#> today
#> without
#> the
#> invaluable
#> help
#> of
#> these
#> people
#> outside
#> of
#> the
#> (former
#> and
#> current)
#> R
#> Core
#> team,
#> who
#> contributed
#> by
#> donating
#> code,
#> bug
#> fixes
#> and
#> documentation:
#> Valerio
#> Aimale,
#> Suharto
#> Anggono,
#> Thomas
#> Baier,
#> Gabe
#> Becker,
#> Henrik
#> Bengtsson,
#> Roger
#> Bivand,
#> Ben
#> Bolker,
#> David
#> Brahm,
#> G"oran
#> Brostr"om,
#> Patrick
#> Burns,
#> Vince
#> Carey,
#> Saikat
#> DebRoy,
#> Matt
#> Dowle,
#> Brian
#> D'Urso,
#> Lyndon
#> Drake,
#> Dirk
#> Eddelbuettel,
#> Claus
#> Ekstrom,
#> Sebastian
#> Fischmeister,
#> John
#> Fox,
#> Paul
#> Gilbert,
#> Yu
#> Gong,
#> Gabor
#> Grothendieck,
#> Frank
#> E
#> Harrell
#> Jr,
#> Peter
#> M.
#> Haverty,
#> Torsten
#> Hothorn,
#> Robert
#> King,
#> Kjetil
#> Kjernsmo,
#> Roger
#> Koenker,
#> Philippe
#> Lambert,
#> Jan
#> de
#> Leeuw,
#> Jim
#> Lindsey,
#> Patrick
#> Lindsey,
#> Catherine
#> Loader,
#> Gordon
#> Maclean,
#> Arni
#> Magnusson,
#> John
#> Maindonald,
#> David
#> Meyer,
#> Ei-ji
#> Nakama,
#> Jens
#> Oehlschl"agel,
#> Steve
#> Oncley,
#> Richard
#> O'Keefe,
#> Hubert
#> Palme,
#> Roger
#> D.
#> Peng,
#> Jose'
#> C.
#> Pinheiro,
#> Tony
#> Plate,
#> Anthony
#> Rossini,
#> Jonathan
#> Rougier,
#> Petr
#> Savicky,
#> Guenther
#> Sawitzki,
#> Marc
#> Schwartz,
#> Arun
#> Srinivasan,
#> Detlef
#> Steuer,
#> Bill
#> Simpson,
#> Gordon
#> Smyth,
#> Adrian
#> Trapletti,
#> Terry
#> Therneau,
#> Rolf
#> Turner,
#> Bill
#> Venables,
#> Gregory
#> R.
#> Warnes,
#> Andreas
#> Weingessel,
#> Morten
#> Welinder,
#> James
#> Wettenhall,
#> Simon
#> Wood,
#> and
#> Achim
#> Zeileis.
#> Others
#> have
#> written
#> code
#> that
#> has
#> been
#> adopted
#> by
#> R
#> and
#> is
#> acknowledged
#> in
#> the
#> code
#> files,
#> including
相关用法
- R stringr str_which 查找匹配索引
- R stringr str_extract 提取完整的匹配项
- R stringr str_subset 查找匹配元素
- R stringr str_escape 转义正则表达式元字符
- R stringr str_trim 删除空格
- R stringr str_sub 使用子字符串的位置获取和设置子字符串
- R stringr str_replace_na 把NA变成“NA”
- R stringr str_trunc 将字符串截断至最大宽度
- R stringr str_match 从匹配中提取组件(捕获组)
- R stringr str_like 以与 SQL 的 LIKE 运算符相同的方式检测模式
- R stringr str_length 计算长度/宽度
- R stringr str_detect 检测是否存在匹配
- R stringr str_count 计算匹配次数
- R stringr str_split 将字符串分成几段
- R stringr str_unique 删除重复的字符串
- R stringr str_remove 删除匹配的模式
- R stringr str_pad 将字符串填充到最小宽度
- R stringr str_equal 判断两个字符串是否相等
- R stringr str_view 查看字符串和匹配项
- R stringr str_glue 用胶水插补
- R stringr str_conv 指定字符串的编码
- R stringr str_order 对字符向量进行排序、排名或排序
- R stringr str_starts 检测开始/结束时是否存在匹配
- R stringr str_c 将多个字符串连接成一个字符串
- R stringr str_dup 复制字符串
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Wrap words into nicely formatted paragraphs。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。