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:
# R program to illustrate # the use of sub() function # Create a string x <- "Geeksforgeeks" # Calling sub() function sub("eek", "ood", x) # Calling sub() with case-sensitivity sub("gee", "Boo", x, ignore.case = FALSE) # Calling sub() with case-insensitivity sub("gee", "Boo", x, ignore.case = TRUE)
Output:
[1] "Goodsforgeeks" [1] "GeeksforBooks" [1] "Booksforgeeks"
Example 2:
# R program to illustrate # the use of sub() function # Create a string x <- c("Geekforgeek", "Geeksforgeeks", "geeksforGeeks") # Calling sub() function sub("Gee", "boo", x) # Calling sub() with case-insensitivity sub("Gee", "boo", x, ignore.case = TRUE)
Output:
[1] "bookforgeek" "booksforgeeks" "geeksforbooks" [1] "bookforgeek" "booksforgeeks" "booksforGeeks"
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.