[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to delete specific characters from a string?

Bazsl

10/11/2007 11:16:00 PM

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.

11 Answers

7stud --

10/11/2007 11:29:00 PM

0

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

Mike Stok

10/11/2007 11:32:00 PM

0


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

The "`Stok' disclaimers" apply.





bob

10/11/2007 11:34:00 PM

0

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

unbewusst.sein

10/11/2007 11:39:00 PM

0

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.

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

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

Bazsl

10/11/2007 11:51:00 PM

0

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.

Giles Bowkett

10/12/2007 2:31:00 AM

0

> >> 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.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

mortee

10/12/2007 4:00:00 AM

0

Giles Bowkett

10/12/2007 4:27:00 AM

0

> > 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.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

Giles Bowkett

10/12/2007 4:30:00 AM

0

> 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.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

James Gray

10/12/2007 12:33:00 PM

0

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