How to delete specific characters from a string?

B

Bazsl

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

7stud --

Bazsl said:
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
 
M

Mike Stok

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 <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
B

Bob Proulx

Bazsl said:
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
 
U

Une Bévue

Bazsl said:
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"
 
B

Bazsl

7stud said:
Bazsl said:
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.
 
G

Giles Bowkett

Is there really no method that allows me to delete N characters starting
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
=> "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/
 
M

mortee

Wayne said:
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.


This little problem is quite enjoyable:

class String
def delete_indices(*indices)
indices.each do |index|
self[index] = ''
end
self
end
end
=> "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
 
G

Giles Bowkett

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/
 
J

James Edward Gray II

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

That makes it easy to reuse the functionality.
s = "muppet" => "muppet"
s[3, 2] = "" => ""
s => "mupt"

James Edward Gray II
 
J

James Edward Gray II

Wayne said:
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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top