字符串 String
R语言中的字符串
R语言中的字符串末尾没有空字符串NULL;
相关函数
grep()函数
grep:globally search a regular expression and print
用法:
grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE)
描述: 在字符串向量x中搜索给定子字符串pattern;
参数:
- pattern:character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector.一个包含正则表达式的子字符串,或者普通的子字符串(参数
fixted = TRUE
) - x:被搜索的字符串向量;
- ignore.case:是否忽略大小写,TRUE:不考虑大小写,FALSE:大小写敏感;
- perl:logical. Should Perl-compatible regexps be used?
- value:if FALSE, a vector containing the (integer) indices of the matches determined by grep is returned, and if TRUE, a vector containing the matching elements themselves is returned.
- 若FALSE:则返回x的数字索引,所对应的元素包含pattern子字符串;
- 若TRUE:则返回x中的元素,这些元素包含pattern子字符串。
- fixed:logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.
- useBytes :logical. If TRUE the matching is done byte-by-byte rather than character-by-character;
- invert:logical. If TRUE return indices or values for elements that do not match.若TRUE,则返回不包含pattern子字符串的元素或索引。
返回值: 一个向量,长度不超过length(x),元素为包含pattern子字符串的x元素的数字索引或者元素本身,如果不包含该子字符串,则会返回空向量。
nchar()函数
用法:
nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)
描述: 获取字符串x或者字符串向量x的长度。 nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
参数:
- x:character vector, or a vector to be coerced to a character vector. Giving a factor is an error.字符串,字符串向量,能被转化成字符串向量的对象,factor会报错
- type:character string: partial matching to one of c("bytes", "chars", "width").
- allowNA:logical: should NA be returned for invalid multibyte strings or "bytes"-encoded strings (rather than throwing an error)?
- keepNA:logical: should NA be returned where ever x is NA? If false, nchar() returns 2, as that is the number of printing characters used when strings are written to output, and nzchar() is TRUE. The default for nchar(), NA, means to use keepNA = TRUE unless type is "width".
返回值:
- 若x为字符串,则返回x的长度;
- 若x为字符串向量,则返回一个向量,每个元素为x对应位置字符串的长度;
注意: 如果x不是字符形式,nchar会得到奇怪的结果
length与nchar:
- length:获取向量、列表等的长度:
length('abc') # 1
- nchar:获取字符串的长度:
nchar('abc') # 3
paste()函数
描述: 把若干个字符串拼接起来.
用法:
paste(..., sep = " ", collapse = NULL)
参数:
- ...:one or more R objects, to be converted to character vectors.需要合并的字符串,可以为字符串,也可以为字符串向量
- sep:a character string to separate the terms. 合并分隔符
- collapse:an optional character string to separate the results.
该参数是针对被合并的对象是字符串向量来说的:
- 若NULL:则返回值为合并后的字符串向量,每个元素为...向量对应元素的合并,分隔符为sep
- 若不为NULL,则返回值为一个长字符串,这是将合并后的字符串向量合并成一个长字符串,分隔符为collapse。
返回值: 一个长字符串或者字符串向量。
说明: 若需要合并的字符串为向量,则长度不够的向量自动补齐