在 R 中替换字符串中的特定字符
在本文中,我们将讨论如何在 R 编程语言中替换字符串中的特定字符。
方法一:使用 gsub()函数
我们可以使用 gsub()函数替换所有出现的特定字符。
Syntax: gsub(character,new_character, string)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
示例: R 程序使用 gsub()函数替换字符中的字符串
R
# consider a string "Hello Geek"
# replace the character 'e' in "Hello
# Geek" with "E"
print(gsub("e", "E", "Hello Geek") )
# consider a string "Python and java"
# replace the character 'a' in "Python
# and java" with "M"
print(gsub("a", "M", "Python and java") )
R
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(sub("e", "E", "Hello Geek") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(sub("a", "M", "Python and java") )
R
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace_all( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace_all("Python and java","a", "M") )
R
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace("Python and java","a", "M") )
输出:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
方法二:使用 sub()函数
我们可以使用 sub()函数仅替换特定字符的第一次出现,它将仅替换字符串的第一个出现字符
Syntax: sub(character,new_character, string)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
示例: R 程序使用 sub()函数替换字符中的字符串
电阻
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(sub("e", "E", "Hello Geek") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(sub("a", "M", "Python and java") )
输出:
[1] "HEllo Geek"
[1] "Python Mnd java"
方法三:使用 str_replace_all()函数
str_replace_all() 也是一个用字符串的特定字符替换字符的函数。它将替换所有出现的字符。它在 stringr 包中可用。所以,我们需要安装和加载包
install: install.packages("stringr")
load: library("stringr")
Syntax: str_replace_all(string, “character”, “new_character”)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
示例: R 程序使用 str_replace_all()函数替换字符中的字符串
电阻
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace_all( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace_all("Python and java","a", "M") )
输出:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
方法四:使用 str_replace()函数
str_replace() 也是一个用字符串的特定字符替换字符的函数。它将仅替换第一次出现。
Syntax: str_replace(string, “character”, “new_character”)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
示例: R 程序使用 str_replace()函数替换字符中的字符串
电阻
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace("Python and java","a", "M") )
输出:
[1] "HEllo Geek"
[1] "Python Mnd java"