1#' Left justify a string. 2#' 3#' Return \code{str} left justified in a string of length \code{width}. 4#' Padding is done using the specified \code{fillchar} (default is an ASCII space). 5#' The original string is returned if \code{width} is less than or equal to \code{nchar(str)}. 6#' 7#' @param str A character vector. 8#' @param width An integer. 9#' @param fillchar A character string. 10#' 11#' @return A character vector. 12#' 13#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.ljust} 14#' 15#' @seealso \code{\link{pystr_rjust}} 16#' 17#' @examples 18#' pystr_ljust("left", 10) 19#' pystr_ljust("left", 10, "*") 20#' 21#' @export 22pystr_ljust <- function(str, width, fillchar=" ") { 23 return(vapply(str, function(x) pystr_ljust_(x, width, fillchar=fillchar), character(1), USE.NAMES = FALSE)) 24} 25 26pystr_ljust_ <- function(str, width, fillchar) { 27 if(width <= nchar(str)) { 28 return(str) 29 } 30 31 return(paste0(str, paste(rep(fillchar, width - nchar(str)), collapse=""))) 32} 33