R语言中字符串的大小写转换——toupper()、tolower()、casefold()和cartr()函数
在本文中,我们将看到 R 编程语言中字符串的大小写转换。
R - toupper()
R语言中的toupper()函数用于将小写字母转换为大写字母。
Syntax: toupper(x)
Parameters:
x: character vector
例子:
R
# R program to illustrate
# uppercase of vectors
# Create example character string
x <- "GeeksforGeeks"
toupper(x)
print(x)
R
# R program to illustrate
# uppercase of vectors
# Create example character string
x <- "GeeksforGeeks"
tolower(x)
print(x)
R
# Create example character string
x <- "GeeksforGeeks"
# Convert to lower case letters
x <- casefold(x, upper = FALSE)
print(x)
R
# Create example character string
x <- "GeeksforGeeks"
# Convert to upper case letters
casefold(x, upper = TRUE)
print(x)
R
# Create example character string
x <- "GeeksforGeeks"
# Translate to upper and lower case
chartr(old = "Geeks", new = "GeEkS", x)
输出:
GEEKSFORGEEKS
在上面的代码中,我们已将字符示例向量转换为大写字符。
R – tolow()函数
tolower()函数用于将大写字母转换为小写字母。
Syntax: tolower(x)
Parameters:
x: character vector
例子:
R
# R program to illustrate
# uppercase of vectors
# Create example character string
x <- "GeeksforGeeks"
tolower(x)
print(x)
输出:
geeksforgeeks
R – casefold()函数
casefold()函数将字符向量和布尔值作为参数。此布尔值决定转换为大写和小写。
Syntax: casefold(x, upper)
Parameters:
- x: Character Vector
- upper: Boolean value for case conversion
示例 1:
R
# Create example character string
x <- "GeeksforGeeks"
# Convert to lower case letters
x <- casefold(x, upper = FALSE)
print(x)
输出:
"geeksforgeeks"
在上面的代码中,将大写的布尔值设置为 FALSE 以将字符向量转换为小写。
示例 2:
R
# Create example character string
x <- "GeeksforGeeks"
# Convert to upper case letters
casefold(x, upper = TRUE)
print(x)
输出:
"GEEKSFORGEEKS"
在上面的代码中,将大写的布尔值设置为 TRUE 以将字符向量转换为大写。
R – cartr()函数
R Programming 中的 cartr()函数在我们必须将一些字符变为大写而一些字符变为小写时使用。
Syntax: cartr(x, old, new)
Parameters:
- x: character vector
- old: a character string specifying the characters to be translated.
- new: a character string specifying the translations
例子:
R
# Create example character string
x <- "GeeksforGeeks"
# Translate to upper and lower case
chartr(old = "Geeks", new = "GeEkS", x)
输出:
"GeEkSforGeeks"