Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

shorten_string

Shorten character strings


Description

This function shortens strings that are longer than max.length chars, without cropping words.

Usage

shorten_string(s, max.length = NULL, abbr = "...")

Arguments

s

A string.

max.length

Maximum length of chars for the string.

abbr

String that will be used as suffix, if s was shortened.

Details

If the string length defined in max.length happens to be inside a word, this word is removed from the returned string (see 'Examples'), so the returned string has a maximum length of max.length, but might be shorter.

Value

A shortened string.

Examples

s <- "This can be considered as very long string!"

# string is shorter than max.length, so returned as is
shorten_string(s, 60)

# string is shortened to as many words that result in
# a string of maximum 20 chars
shorten_string(s, 20)

# string including "considered" is exactly of length 22 chars
shorten_string(s, 22)

sjmisc

Data and Variable Transformation Functions

v2.8.6
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>), Iago Giné-Vázquez [ctb], Alexander Bartel [ctb] (<https://orcid.org/0000-0002-1280-6138>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.