Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

ruby replace words in a string

# Replace first value a return new string
sentence = 'pop poper'
sentence.sub('pop', 'stop')
=> 'stop poper'
puts sentence
=> 'pop poper'

# Replace all matches and return new string
sentence = 'pop poper'
sentence.gsub('pop', 'stop')
=> 'stop stoper'
puts sentence
=> 'pop poper'

# Replace first value and update existing string
sentence = 'pop poper'
sentence.sub!('pop', 'stop')
=> 'stop poper'
puts sentence
=> 'stop poper'

# Replace all matches and update string
sentence = 'pop poper'
sentence.gsub!('pop', 'stop')
=> 'stop stoper'
puts sentence
=> 'stop stoper'
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ruby #replace #words #string
ADD COMMENT
Topic
Name
5+6 =