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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

with String#slice ???
<class String - RDoc Documentation;

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"

···

Bazsl <hs@dbginc.com> 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.

--
Une Bévue

7stud -- wrote:

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
  

Thanks. Very elegant.

>> 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 Bowkett

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

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 Bowkett

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

···

On 10/11/07, Giles Bowkett <gilesb@gmail.com> 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.

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.

>> s = "muppet"
=> "muppet"
>> s[3, 2] = ""
=> ""
>> s
=> "mupt"
>>

James Edward Gray II

···

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

Wayne E. Seguin wrote:

···

On 10/11/07, Giles Bowkett <gilesb@gmail.com> wrote:

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 Bowkett

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"

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

> 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 Bowkett

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

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 Bowkett

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

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

  ~Wayne

···

On 10/12/07, Giles Bowkett <gilesb@gmail.com> wrote:

> 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 Bowkett

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 Gray II

···

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

Wayne E. Seguin wrote:

On 10/11/07, Giles Bowkett <gilesb@gmail.com> wrote:

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 Bowkett

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"

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.

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 10/12/07, Wayne E. Seguin <wayneeseguin@gmail.com> wrote:

Very nice.

···

On 10/12/07, James Edward Gray II <james@grayproductions.net> wrote:

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 Gray II