Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to delete specific characters from a string?

Reply
Thread Tools

How to delete specific characters from a string?

 
 
James Edward Gray II
Guest
Posts: n/a
 
      10-12-2007
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


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      10-12-2007
On Oct 11, 2007, at 11:00 PM, mortee wrote:

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

>> => "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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
XML parsing problem finding a specific element in a specific place mazdotnet ASP .Net 2 10-02-2009 10:07 AM
Is ViwState Page-Specific or UserControl-Specific =?Utf-8?B?SmF2?= ASP .Net 2 08-16-2006 09:30 PM
redirect traffic on specific ip to specific interface mimiseh Cisco 3 06-05-2005 09:14 PM
How do you make sure a frameset is loaded? I'm trying to open a frameset in a new window which shows a specific html page in a specific frame ck388 Javascript 1 09-24-2003 08:32 PM



Advertisments