sub
function in R Language is used to replace the first match of a pattern in a string. If there is a vector of string elements, then it will replace the first match of the pattern from all elements.
Syntax: sub(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"
sub( "eek" , "ood" , x)
sub( "gee" , "Boo" , x, ignore.case = FALSE)
sub( "gee" , "Boo" , x, ignore.case = TRUE)
|
Output:
[1] "Goodsforgeeks"
[1] "GeeksforBooks"
[1] "Booksforgeeks"
Example 2:
x < - c( "Geekforgeek" , "Geeksforgeeks" , "geeksforGeeks" )
sub( "Gee" , "boo" , x)
sub( "Gee" , "boo" , x, ignore.case = TRUE)
|
Output:
[1] "bookforgeek" "booksforgeeks" "geeksforbooks"
[1] "bookforgeek" "booksforgeeks" "booksforGeeks"
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!