Gherman
Gherman

Reputation: 7436

How to delete n characters from m position from a string in Ruby?

For some reason most Ruby string functions work with predetermined substrings and patterns. I don't have any assumptions about the string's contents, but I need to just delete n chars from m-th position. How do I do that?

Upvotes: 0

Views: 92

Answers (2)

Pavan
Pavan

Reputation: 33552

You can use slice!

str='Apple'

str.slice![1..3] #=> ppl

puts str

#=> Ae

Upvotes: 0

steenslag
steenslag

Reputation: 80085

str = "abcdefghij"
m = 4
n = 3
str.slice!(m,n) # => "efg"
p str # => "abcdhij"  

Upvotes: 5

Related Questions