拆分为数组

基本类型 / 字符串

str_data <- "1981|Kim Victoria|engineer"

# 列表:每个输入字符串对应一个元素;fixed = TRUE 时 "|" 是普通字符
lst_data <- strsplit(str_data, "|",
                     fixed = TRUE)[[1]]

# 每个部分一个名称
year <- as.integer(lst_data[1])
name <- lst_data[2]
position <- lst_data[3]

# 分隔符是正则表达式:一串连续空格
words <- strsplit("red  green   blue", " +")[[1]]

# 只用单个空格会产生空字符串
spaced <- strsplit("red  green", " ",
                   fixed = TRUE)[[1]]

# 不加 fixed = TRUE 时,"|" 是正则表达式
wrong <- strsplit("a|b", "|")[[1]]

# 末尾的空字符串会被丢弃
trailing <- strsplit("a,b,", ",",
                     fixed = TRUE)[[1]]

str(lst_data)
cat("year =", year, "\n")
cat("name =", name, "\n")
cat("position =", position, "\n")
str(words)
str(spaced)
str(wrong)
str(trailing)