Replace all the matches of a Pattern from a String in R Programming – gsub() Function
gsub()
function in R Language is used to replace all the matches of a pattern from a string. If the pattern is not found the string will be returned as it is.
Syntax:
gsub(pattern, replacement, string, ignore.case=TRUE/FALSE)
Parameters:
pattern: string to be matched
replacement: string for replacement
string: String or String vector
ignore.case: Boolean value for case-sensitive replacement
Example 1:
x < - "Geeksforgeeks"
gsub( "eek" , "ood" , x)
gsub( "gee" , "Boo" , x, ignore.case = FALSE)
gsub( "gee" , "Boo" , x, ignore.case = TRUE)
|
Output:
[1] "Goodsforgoods"
[1] "GeeksforBooks"
[1] "BooksforBooks"
Example 2:
x < - c( "R Example" , "Java example" , "Go-Lang Example" )
gsub( "Example" , "Tutorial" , x)
gsub( "Example" , "Tutorial" , x, ignore.case = TRUE)
|
Output:
[1] "R Tutorial" "Java example" "Go-Lang Tutorial"
[1] "R Tutorial" "Java Tutorial" "Go-Lang Tutorial"
Last Updated :
05 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...