How to use str_replace_all() function In R Language
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 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" ) ) |
Output:
[1] "HEllo GEEk" [1] "Python Mnd jMvM"
Replace Specific Characters in String in R
In this article, we will discuss how to replace specific characters in a string in R Programming Language.