To replace the first two characters of a string with 'Hello': : sub with regular expressions « String « Ruby
- Ruby
- String
- sub with regular expressions
To replace the first two characters of a string with 'Hello':
x = "This is a test"
puts x.sub(/^../, 'Hello')
Related examples in the same category