string_fill {stringmagic} | R Documentation |
Fills a character string up to a size
Description
Fills a character string up to a size and handles multibyte encodings (differently from sprintf).
Usage
string_fill(
x = "",
n = NULL,
symbol = " ",
right = FALSE,
center = FALSE,
na = "NA"
)
Arguments
x |
A character vector. |
n |
Integer scalar, possibly equal to |
symbol |
Character scalar of length 1, default is a space (" "). It is the symbol with which the string will be filled. |
right |
Logical scalar, default is |
center |
Logical scalar, default is |
na |
Character scalar or |
Details
If you use character filling of the form sprintf("% 20s", x)
with x``containing multibyte characters, you may be suprised that all character strings do not end up at the same lenght (the occurrence of this problem depends on many things: encodings are a mess).
string_fill'
uses only base R functions to compensate this. It is slightly slower but, in general, safer.
It also looks a bit like base::format()
, but slightly different (and a bit faster, but more restrictive).
Value
This functions returns a character vector of the same lenght as the vector in input.
Author(s)
Laurent R. Berge
See Also
String operations: string_is()
, string_get()
, string_clean()
, string_split2df()
.
Chain basic operations with string_ops()
. Clean character vectors efficiently
with string_clean()
.
Use string_vec()
to create simple string vectors.
String interpolation combined with operation chaining: string_magic()
. You can change string_magic
default values with string_magic_alias()
and add custom operations with string_magic_register_fun()
.
Display messages while benefiting from string_magic
interpolation with cat_magic()
and message_magic()
.
Other tools with aliases:
cat_magic_alias()
,
string_magic()
,
string_magic_alias()
,
string_ops_alias()
,
string_vec_alias()
Examples
x = c("apple", "pineapple")
# simple fill with blank
cat(paste0(string_fill(x), ":", c(3, 7), "€"), sep = "\n")
# center fill
cat(paste0(string_fill(x, center = TRUE), ":", c(3, 7), "€"), sep = "\n")
# changing the length of the fill and the symbol used for filling
cat(paste0(string_fill(x), ":",
string_fill(c(3, 7), 3, "0", right = TRUE), "€"), sep = "\n")
# na behavior: default/NA/other
x = c("hello", NA)
string_fill(x)
string_fill(x, na = NA)
string_fill(x, na = "(missing)")