truncate_bytes: Truncate a string to be no longer than a specific number of...

Description Usage Arguments Value Note See Also Examples

View source: R/convenience.r

Description

Truncate a string to be no longer than a specific number of bytes

Usage

1
truncate_bytes(x, max_len = Inf)

Arguments

x

Vector of strings to truncate

max_len

Max length (in bytes). Can be one number or have the same lenth as x

Value

x, truncated to max_len bytes each. The function won't chop multi-byte characters in half, so the result might be shorter than max_len

Note

Requires package stringi. This implementation is really inefficient.

See Also

stringi::stri_sub() and base::substr()

Examples

1
2
3
4
5
6
truncate_bytes(c("ab", "cde"), c(1, 2))
latin1_str <- "fa\xE7ile"
Encoding(latin1_str) <- "latin1"
truncate_bytes(latin1_str, 3)
truncate_bytes("ὯaὯa", 2) # empty string
truncate_bytes("ὯaὯa", 6) # Ὧa only

karldw/kdw.junk documentation built on Sept. 8, 2021, 9:30 p.m.