value = "abc abc"
puts value # abc abc
# Sub replaces just the first instance.
value = value.sub("abc", "---")
puts value # --- abc
# Gsub replaces all instances.
value = value.gsub("abc", "---")
puts value # --- ---
sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
sub("4", "8", "An Introduction to R Software Course will be of 4 weeks duration" )
##"An Introduction to R Software Course will be of 8 weeks duration"
gsub("4", "8", "An Introduction to R Software Course will be of 4 weeks duration" )
##"An Introduction to R Software Course will be of 8 weeks duration"
4条答案
按热度按时间bgtovc5b1#
g
代表全局,如替换全局(全部):IRB中:
2ic8powd2#
不同之处在于
sub
只替换指定模式的第一个示例,而gsub
对所有示例都进行替换(即全局替换)。4szc88ey3#
jc3wubiy4#
sub
和gsub
分别执行第一个和所有匹配的替换。