How to delete specific characters from a string?

Discussion in 'Ruby' started by Bazsl, Oct 12, 2007.

  1. Bazsl

    Bazsl Guest

    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, Oct 12, 2007
    #1
    1. Advertising

  2. Bazsl

    7stud -- Guest

    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/.
    7stud --, Oct 12, 2007
    #2
    1. Advertising

  3. Bazsl

    Mike Stok Guest

    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 Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
    Mike Stok, Oct 12, 2007
    #3
  4. Bazsl

    Bob Proulx Guest

    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
    Bob Proulx, Oct 12, 2007
    #4
  5. Bazsl

    Une Bévue Guest

    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.


    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"


    --
    Une Bévue
    Une Bévue, Oct 12, 2007
    #5
  6. Bazsl

    Bazsl Guest

    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.
    Bazsl, Oct 12, 2007
    #6
  7. > >> 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/
    Giles Bowkett, Oct 12, 2007
    #7
  8. Bazsl

    mortee Guest

    Wayne E. Seguin wrote:
    > On 10/11/07, Giles Bowkett <> 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
    mortee, Oct 12, 2007
    #8
  9. > > 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/
    Giles Bowkett, Oct 12, 2007
    #9
  10. > 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/
    Giles Bowkett, Oct 12, 2007
    #10
  11. On Oct 11, 2007, at 9:30 PM, Giles Bowkett 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
    James Edward Gray II, Oct 12, 2007
    #11
  12. On Oct 11, 2007, at 11:00 PM, mortee wrote:

    > Wayne E. Seguin wrote:
    >> On 10/11/07, Giles Bowkett <> 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.


    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
    James Edward Gray II, Oct 12, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    2
    Views:
    2,431
    Jerry
    Jan 13, 2005
  2. Behrang Dadsetan

    Deleting specific characters from a string

    Behrang Dadsetan, Jul 9, 2003, in forum: Python
    Replies:
    17
    Views:
    712
    Bengt Richter
    Jul 11, 2003
  3. Michael Chermside

    RE: Deleting specific characters from a string

    Michael Chermside, Jul 10, 2003, in forum: Python
    Replies:
    1
    Views:
    314
    Paul Rudin
    Jul 10, 2003
  4. Angus
    Replies:
    16
    Views:
    578
    Tim Rentsch
    Nov 18, 2010
  5. CES
    Replies:
    2
    Views:
    74
    Kevin Newman
    Jul 27, 2005
Loading...

Share This Page