Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Ruby)  »  How do you get the tail end of a string?...
Page 1 of 2    Goto page 1, 2  Next

How do you get the tail end of a string?...

Author Message
Just Another Victim of the Ambient Morality...
Posted: Fri Oct 30, 2009 5:17 am
Guest
I'm actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way. This happens to me a lot and I
really appreciate Python's curt syntax in this case: string[index:]
What's the Ruby way?
Thank you!
 
Michael W. Ryder...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Just Another Victim of the Ambient Morality wrote:
Quote:
I'm actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way. This happens to me a lot and I
really appreciate Python's curt syntax in this case: string[index:]
What's the Ruby way?
Thank you!


You mean like string[-1]?
 
7stud --...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Quote:
I'm actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way. This happens to me a lot and I
really appreciate Python's curt syntax in this case: string[index:]
What's the Ruby way?

Thank you!

str = "hello"
result = str[1..-1]

p result
--output:--
"ello"
--
Posted via http://www.ruby-forum.com/.
 
Rajinder Yadav...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Just Another Victim of the Ambient Morality wrote:
Quote:
I'm actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I've figured out is this:

using reg-ex


irb(main):102:0> "quick brown fox".scan /.$/
=> ["x"]

irb(main):103:0> "yo mama".scan /.$/
=> ["a"]


See Reg-ex section here: http://www.ruby-doc.org/docs/ProgrammingRuby/


You can convert the string into an array, and use Array.last and Array.pop
methods to consume from the end

irb(main):090:0> letters = "quick brown fox".scan /./
=> ["q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x"]

irb(main):091:0> letters.last
=> "x"
irb(main):092:0> letters.pop
=> "x"

irb(main):093:0> letters.last
=> "o"
irb(main):094:0> letters.pop
=> "o"

irb(main):095:0> letters.last
=> "f"
irb(main):096:0> letters.pop
=> "f"

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.
 
Michael W. Ryder...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Michael W. Ryder wrote:
Quote:
Just Another Victim of the Ambient Morality wrote:
I'm actually hoping this is an embarrassing question but how do
you get the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way. This happens to me a lot and
I really appreciate Python's curt syntax in this case: string[index:]
What's the Ruby way?
Thank you!

You mean like string[-1]?

I'm sorry, I missed the part about the index number of characters. Try
string[-index, index].
 
7stud --...
Posted: Fri Oct 30, 2009 5:17 am
Guest
7stud -- wrote:
Quote:
I'm actually hoping this is an embarrassing question but how do you get
the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way. This happens to me a lot and I
really appreciate Python's curt syntax in this case: string[index:]
What's the Ruby way?

Thank you!

str = "hello"
result = str[1..-1]

p result
--output:--
"ello"

...which is very similar to python--except python doesn't include the
last index position in the slice:

str = "hello"
result = str[1:-1]
print result

--output:--
ell
--
Posted via http://www.ruby-forum.com/.
 
Phrogz...
Posted: Fri Oct 30, 2009 10:27 pm
Guest
On Oct 30, 5:11 am, Bertram Scharpf <li... at (no spam) bertram-scharpf.de> wrote:
Quote:
Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael W. Ryder wrote:
Just Another Victim of the Ambient Morality wrote:
    I'm actually hoping this is an embarrassing question but how do you
get the tail end of a string?  All I've figured out is this:

index = 4
string[index, string.size - index]

    ...but surely there's a better way.

You mean like string[-1]?

I'm sorry, I missed the part about the index number of characters.  Try
string[-index, index].

That's really ugly. You shouldn't have to mention `index' twice.
I would even support a language extension to solve this problem.

No need for any extensions.

irb(main):001:0> "123456"[-4..-1]
=> "3456"
 
Phrogz...
Posted: Fri Oct 30, 2009 10:30 pm
Guest
On Oct 30, 10:40 am, Bertram Scharpf <li... at (no spam) bertram-scharpf.de> wrote:
Quote:
  irb(main):001:0> def timer ; start = Time.now ; yield ; Time.now - start ; end
  => nil
  irb(main):002:0> index = 2
  => 2
  irb(main):003:0> timer do 100_000.times { "hello"[-index..-1] } end
  => 0.888826
  irb(main):004:0> timer do 100_000.times { "hello".tail index } end
  => 0.21357

Your calculation needs more than four times as much processor
time. Don't you need to save time?

Really? You're arguing about the efficiency of something that is about
6.8µs slower?
I don't personally need to save the time. Nor do you, unless you have
a real-world program using this method that you have benchmarked and
found this particular method call to be a significant portion of your
execution time.
 
Michael W. Ryder...
Posted: Fri Oct 30, 2009 10:56 pm
Guest
Bertram Scharpf wrote:
Quote:
Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael W. Ryder wrote:
Just Another Victim of the Ambient Morality wrote:
I'm actually hoping this is an embarrassing question but how do you
get the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.

You mean like string[-1]?
I'm sorry, I missed the part about the index number of characters. Try
string[-index, index].

That's really ugly. You shouldn't have to mention `index' twice.
I would even support a language extension to solve this problem.

For my private use I wrote a suite of methods

String#head
String#tail
String#starts_with
String#ends_with


Just out of curiosity, do your methods work in a case like:
a="123456"
index = 12
puts a(-index, index)

This is the only place where my code fails as it does not check to make
sure that index is not greater than a.length.

Quote:
Probably I should make an own gem of it. I'll think about that
this weekend. For so long have a look at my personal library:

http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.7/rdoc/classes/String.html

By the way: Still I'm convinced that there should be a
`String#notempty?' method corresponding to `Numeric#nonzero?'.

Bertram

 
Rajinder Yadav...
Posted: Fri Oct 30, 2009 11:13 pm
Guest
On Fri, Oct 30, 2009 at 6:49 AM, Bertram Scharpf
<lists at (no spam) bertram-scharpf.de> wrote:
Quote:
Hi,

Am Freitag, 30. Okt 2009, 13:35:24 +0900 schrieb Rajinder Yadav:
Just Another Victim of the Ambient Morality wrote:
    I'm actually hoping this is an embarrassing question but how do you
get the tail end of a string?  All I've figured out is this:
using reg-ex

irb(main):102:0> "quick brown fox".scan /.$/
=> ["x"]

Why scan? There's just one match.

 "quick brown fox"[ /.\z/]

I like this and need to revisit a few things, thanks =)

Quote:
Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de





--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives
 
Bertram Scharpf...
Posted: Fri Oct 30, 2009 11:20 pm
Guest
Hi,

Am Samstag, 31. Okt 2009, 04:00:09 +0900 schrieb Michael W. Ryder:
Quote:
Bertram Scharpf wrote:
For my private use I wrote a suite of methods
String#head
String#tail

Just out of curiosity, do your methods work in a case like:
a="123456"
index = 12
puts a(-index, index)

Sure they do.

main:003> a = "123456"
=> "123456"
main:004> a.head 5
=> "12345"
main:005> a.tail 5
=> "23456"
main:006> a.head 12
=> "123456"
main:007> a.tail 8
=> "123456"

I remember that I detected this effect just when I wrote them and
that I decided against the a[-i,i] behaviour.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
 
Phrogz...
Posted: Sun Nov 01, 2009 5:48 am
Guest
On Oct 31, 10:35 pm, "Just Another Victim of the Ambient Morality"
<ihates... at (no spam) hotmail.com> wrote:
Quote:
[...] As others have pointed out, it's:
string[-index..-1]
[...] It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once.  I'm very surprised that Ruby does not have this level of
expressiveness...

Is what I've done above a misquote? (Both these statements seem
attributed to you in the same message on comp.lang.ruby.)

Assuming it's not a misquote: how is the code you wrote on the second
quoted line above not exactly what you're "surprised that ruby does
not have"?
 
Just Another Victim of the Ambient Morality...
Posted: Sun Nov 01, 2009 6:02 am
Guest
"David A. Black" <dblack at (no spam) rubypal.com> wrote in message
news:alpine.LFD.2.00.0910300803050.7908 at (no spam) rubypal.com...
Quote:

On Fri, 30 Oct 2009, Bertram Scharpf wrote:
Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael W. Ryder wrote:
Just Another Victim of the Ambient Morality wrote:
I'm actually hoping this is an embarrassing question but how do
you
get the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.

You mean like string[-1]?

I'm sorry, I missed the part about the index number of characters. Try
string[-index, index].

That's really ugly. You shouldn't have to mention `index' twice.

I don't think there's anything wrong with it. It works well, and
there's nothing stylistically wrong with using a local variable twice.
If index is a method that does a (re)calculation every time, you'd
want to cache it, but that's not the case in the example.

There's nothing wrong with it other than it doesn't work. As others
have pointed out, it's:

string[-index..-1]

However, I'm more concerned with your general attitude about language
structure. Even if it did work, you can say the same thing for my original
solution:

string[index, string.size - index]

It works well... except that it uses both index _and_ string variables
twice.
Am Freitag is right. It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I'm very surprised that Ruby does not have this level of
expressiveness...
 
Daniel Danopia...
Posted: Sun Nov 01, 2009 6:49 pm
Guest
On Oct 30, 12:21 pm, Bertram Scharpf <li... at (no spam) bertram-scharpf.de> wrote:
Quote:
Hi Aldric,

Am Samstag, 31. Okt 2009, 01:46:10 +0900 schrieb Aldric Giacomoni:

Bertram Scharpf wrote:
String#notempty?

Because some behavior is not there by default doesn't mean it's
a bias.

That's what I wanted to say: I am convinced it should be there by
default. It's just a few lines of code. It's pure logic when you
have Numeric#nonzero? and it's very useful to build default
values:

  str.notempty? || "(none)"    # analoigous to SQL-coalesce()


So basically "String#notempty?" would be the same thing as
"String#any?" ?
 
Michael W. Ryder...
Posted: Mon Nov 02, 2009 3:35 am
Guest
Just Another Victim of the Ambient Morality wrote:
Quote:
"David A. Black" <dblack at (no spam) rubypal.com> wrote in message
news:alpine.LFD.2.00.0910300803050.7908 at (no spam) rubypal.com...
On Fri, 30 Oct 2009, Bertram Scharpf wrote:
Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael W. Ryder wrote:
Just Another Victim of the Ambient Morality wrote:
I'm actually hoping this is an embarrassing question but how do
you
get the tail end of a string? All I've figured out is this:

index = 4
string[index, string.size - index]

...but surely there's a better way.

You mean like string[-1]?
I'm sorry, I missed the part about the index number of characters. Try
string[-index, index].
That's really ugly. You shouldn't have to mention `index' twice.
I don't think there's anything wrong with it. It works well, and
there's nothing stylistically wrong with using a local variable twice.
If index is a method that does a (re)calculation every time, you'd
want to cache it, but that's not the case in the example.

There's nothing wrong with it other than it doesn't work.


What method doesn't work? If you mean the string[-index, index] method
it does work fine for me. I am using 1.9.1 if that makes any
difference. The string[-index..-1] method does the same thing but I
have used Business Basic for over 20 years so my method was easier for me.


As others
Quote:
have pointed out, it's:

string[-index..-1]

However, I'm more concerned with your general attitude about language
structure. Even if it did work, you can say the same thing for my original
solution:

string[index, string.size - index]

It works well... except that it uses both index _and_ string variables
twice.
Am Freitag is right. It would be better if there were some method of
getting the tail end of a string by only mentioning the needed parameters
once. I'm very surprised that Ruby does not have this level of
expressiveness...

 
 
Page 1 of 2    Goto page 1, 2  Next
All times are GMT
The time now is Sat Dec 05, 2009 11:17 pm