How to delete specific characters from a string?

Is there really no method that allows me to delete N characters starting
at position P from a string? I have looked (carefully I hope) through
the String methods and did not see a way to do this. Thanks.

Bazsl wrote:

Is there really no method that allows me to delete N characters starting
at position P from a string? I have looked (carefully I hope) through
the String methods and did not see a way to do this. Thanks.

Try this:

str = “hello world”
str[0, 6] = ‘’
puts str

On 11-Oct-07, at 7:15 PM, Bazsl wrote:

Is there really no method that allows me to delete N characters
starting at position P from a string? I have looked (carefully I
hope) through the String methods and did not see a way to do this.
Thanks.

bash-3.2$ irb
irb(main):001:0> s = ‘abcdefghi’
=> “abcdefghi”
irb(main):002:0> s[3,4] = ‘’
=> “”
irb(main):003:0> s
=> “abchi”

might be one way to do it.

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Bazsl [email protected] wrote:

Is there really no method that allows me to delete N characters starting
at position P from a string? I have looked (carefully I hope) through
the String methods and did not see a way to do this. Thanks.

with String#slice ???
http://www.ruby-doc.org/core/classes/String.html#M000858

may be :
str.slice!(fixnum, fixnum) => new_str or nil

string = “this is a string”
p string.slice!( 12, 4 )

=> “ring”

p string.slice!( 5, 2 )

=> “is”

7stud – wrote:

str[0, 6] = ‘’
puts str

Thanks. Very elegant.

Bazsl wrote:

Is there really no method that allows me to delete N characters starting
at position P from a string? I have looked (carefully I hope) through
the String methods and did not see a way to do this. Thanks.

Look at the String#slice method. This is usually used via the []
operator routines.

ri “String#slice”

    a = "hello there"
    a[1]                   #=> 101
    a[1,3]                 #=> "ell"
    a[1..3]                #=> "ell"
    a[-3,2]                #=> "er"
    a[-4..-2]              #=> "her"
    a[12..-1]              #=> nil
    a[-2..-4]              #=> ""
    a[/[aeiou](.)\1/]      #=> "ell"
    a[/[aeiou](.)\1/, 0]   #=> "ell"
    a[/[aeiou](.)\1/, 1]   #=> "l"
    a[/[aeiou](.)\1/, 2]   #=> nil
    a["lo"]                #=> "lo"
    a["bye"]               #=> nil

Bob

Is there really no method that allows me to delete N characters starting
at position P from a string? I have looked (carefully I hope) through
the String methods and did not see a way to do this. Thanks.

I think the OP was looking for a method on String itself, but the
whole point of Ruby is that if the language doesn’t have the features
you want, you just add the features to the language.

class String
def delete_n_from_p(n, p)
n.times do
self[p] = ‘’
end
self
end
end

“muppet”.delete_n_from_p(2,3)
=> “mupt”

That makes it easy to reuse the functionality.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

On 10/11/07, Giles B. [email protected] wrote:

self

This little problem is quite enjoyable:

class String
def delete_indices(*indices)
indices.each do |index|
self[index] = ‘’
end
self
end
end

a = “Testing String”
=> “Testing String”

a.delete_indices(0,3,6,8)
=> “estng trng”

HTH,

~Wayne

I think the OP was looking for a method on String itself, but the
whole point of Ruby is that if the language doesn’t have the features
you want, you just add the features to the language.

This little problem is quite enjoyable:

class String
def delete_indices(*indices)
indices.each do |index|
self[index] = ‘’
end
self
end
end

What’s neat about that solution is that you can pass it a list or a
range.

“muppet”.delete_indices(2,4)
=> “mupe”

“muppet”.delete_indices(2…4)
=> “mut”


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

Wayne E. Seguin wrote:

self

end
end

a = “Testing String”
=> “Testing String”

a.delete_indices(0,3,6,8)
=> “estng trng”

What’s your point? Your code probably won’t do what it seems to intend
to, because the characters shift to the left during the process.
However, Giles’ code seems to be OK, because it deletes from the same
position, n times, which’ll do what it’s supposed to.

mortee

On 10/12/07, Giles B. [email protected] wrote:

end

What’s neat about that solution is that you can pass it a list or a range.

“muppet”.delete_indices(2,4)
=> “mupe”

“muppet”.delete_indices(2…4)
=> “mut”


Giles B.

I love that observation, great point Giles! Truly enjoyable.

~Wayne

What’s your point? Your code probably won’t do what it seems to intend
to, because the characters shift to the left during the process.
However, Giles’ code seems to be OK, because it deletes from the same
position, n times, which’ll do what it’s supposed to.

the point is probably just having fun. but the way to be sure your
code does what you want is to give it a spec with RSpec.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

On 10/12/07, Wayne E. Seguin [email protected] wrote:

I love that observation, great point Giles! Truly enjoyable.

class String
def delete_indices(*indices, &block)
indices.each do |index|
yield self[index] if block_given?
self[index] = ‘’
end
self
end
end

Delete elements in this order:

“Testing String”.delete_indices(0,2,6,8)
=> “esing trng”

Delete elements in a specified order and do something with them:

“Testing String”.delete_indices(0,2,6,8) {|value| p value.chr}
“T”
“t”
“S”
“i”
=> “esing trng”

Delete a range and do something with it:

“Testing String”.delete_indices(0…4) {|value| p value}
“Testi”
=> “ng String”

Mix and match:

“Testing String”.delete_indices(0,2…4,8) {|value| p value}
84
“tin”
110
=> “esg Strig”

~Wayne

P.S. Just for fun. Giles already answered the OPs question IMO so why
not
play around with it? One of the things I love about Ruby is that you can
actually enjoy playing around with the language itself!

On Oct 11, 2007, at 9:30 PM, Giles B. wrote:

class String

That makes it easy to reuse the functionality.

s = “muppet”
=> “muppet”

s[3, 2] = “”
=> “”

s
=> “mupt”

James Edward G. II

On Oct 11, 2007, at 11:00 PM, mortee wrote:

  self[p] = ''


end
to, because the characters shift to the left during the process.
You can fix the ordering issue above by changing the line:

indices.each do |index|

to:

indices.sort { b <=> a }.each do |index|

Fixing for range usage is more complicated though.

James Edward G. II

On 10/12/07, James Edward G. II [email protected] wrote:

James Edward G. II

Very nice.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs