What is the difference between gsub and sub methods for Ruby Strings
今天,我一直在仔细阅读
在irb中:
1 2 3 4 | >>"hello".sub('l', '*') =>"he*lo" >>"hello".gsub('l', '*') =>"he**o" |
区别在于
1 2 3 4 5 6 7 8 | 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 # --- --- |
1 2 3 4 5 6 7 8 9 10 11 12 | 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" |