Replace Specific Characters in String in R - GeeksforGeeks

23 Sep, 2021
In this article, we will discuss how to replace specific characters in a string in R Programming Language.
We can replace all occurrences of a particular character using gsub() function.
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
Example: R program to replace character in a string using gsub() function
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") )
Output:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
We can replace only the first occurrence of a particular character using sub() function, it will replace only the first occurrence character in the string
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
Example: R program to replace character in a string using sub() function
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") )
Output:
[1] "HEllo Geek"
[1] "Python Mnd java"
str_replace_all() is also a function that replaces the character with a particular character in a string. It will replace all occurrences of the character. It is available in stringr package. So, we need to install and load the package
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
Example: R program to replace character in a string using str_replace_all() function
R
# load the stringr packagelibrary("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") )
Output:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
str_replace() is also a function that replaces the character with a particular character in a string. It will replace only the first occurrence.
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
Example: R program to replace character in a string using str_replace() function
R
# load the stringr packagelibrary("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") )
Output:
[1] "HEllo Geek"
[1] "Python Mnd java"
Picked
R String-Programs
R-strings
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Replace specific values in column in R DataFrame ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?