字符串重复

基本类型 / 字符串

str_v <- strrep("7", 3)
# str_v = 777

# 任何字符串都能重复,不限于单个字符
laugh <- strrep("ha", 3)
# laugh = hahaha

# 重复 0 次:得到空字符串
empty <- strrep("7", 0)
# empty =

# 次数向量:每个次数对应一个结果
stairs <- strrep("ab", 1:3)
# stairs = ab abab ababab

cat("str_v =", str_v, "\n")
cat("laugh =", laugh, "\n")
cat("empty =", empty, "\n")
cat("stairs =", stairs, "\n")

# 负的次数会报错
neg_error <- tryCatch(strrep("7", -1),
                      error = conditionMessage)
cat("neg_error =", neg_error, "\n")
# neg_error = invalid 'times' value