How do you get the tail end of a string?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

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!
 
M

Michael W. Ryder

Just said:
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]?
 
M

Michael W. Ryder

Michael said:
Just said:
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].
 
7

7stud --

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

7stud --

7stud said:
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
 
R

Rajinder Yadav

Just said:
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.
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 13:35:24 +0900 schrieb Rajinder Yadav:
Just said:
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/]

Bertram
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael said:
Just said:
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

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
 
A

Albert Schlef

Bertram said:
For my private use I wrote a suite of methods [snip]
String#starts_with
String#ends_with

What's wrong with the built-in #start_with and #end_with, except the
incorrect grammar?
 
T

Thomas Preymesser

[Note: parts of this message were removed to make it a legal post.]

2009/10/30 Just Another Victim of the Ambient Morality <
(e-mail address removed)>
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]

is equivalent to

string[index..-1]

-Thomas


--
Thomas Preymesser
(e-mail address removed)
http://thopre.googlepages.com/
http://thopre.wordpress.com/

Pablo Picasso<http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html>
- "Computers are useless. They can only give you answers."
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 20:53:02 +0900 schrieb Albert Schlef:
Bertram said:
For my private use I wrote a suite of methods [snip]
String#starts_with
String#ends_with

What's wrong with the built-in #start_with and #end_with, except the
incorrect grammar?

That's Ruby 1.8.7/1.9; I wrote it years ago.

It is satisfying me and it is giving me hope that parts of my
proposal are already realized.

Bertram
 
D

David A. Black

Hi --

Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
Michael said:
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.


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
A

Aldric Giacomoni

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

class String
def notempty?
!self.empty?
end
end

Good enough?
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 22:32:19 +0900 schrieb Aldric Giacomoni:
class String
def notempty?
!self.empty?
end
end

My question was not _how_ to implement it but _why_ _not_ to add
it to the interpreter.

Your method is returning `true' what Numeric#nonzero? will never
do.

Bertram
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 21:06:11 +0900 schrieb David A. Black:
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.

What when it's a method call?

Bertram
 
A

Aldric Giacomoni

Bertram said:
My question was not _how_ to implement it but _why_ _not_ to add
it to the interpreter.

Your method is returning `true' what Numeric#nonzero? will never
do.

Bertram

You want a method which returns "nil" if the string is empty, and the
string itself if the string is nonempty?

class String
def nonempty?
self.empty? ? nil : self
end
end

Happy? ;-)
Why wouldn't this be implemented by default, er, I don't know, I really
can't think of a time when I'd need this type of behavior.
 
D

David A. Black

Hi --

Hi,

Am Freitag, 30. Okt 2009, 21:06:11 +0900 schrieb David A. Black:
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.

What when it's a method call?

Well, that would be the case I addressed in the part of my paragraph
you deleted :)


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
M

Marnen Laibow-Koser

Bertram said:
Hi,

Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
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.

And you don't. string[-index..-1] does the trick.
I would even support a language extension to solve this problem.

No need.

Best,
 
B

Bertram Scharpf

Hi,

Am Samstag, 31. Okt 2009, 00:12:01 +0900 schrieb Marnen Laibow-Koser:
Bertram said:
Am Freitag, 30. Okt 2009, 13:25:05 +0900 schrieb Michael W. Ryder:
string[-index, index].

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

And you don't. string[-index..-1] does the trick.
I would even support a language extension to solve this problem.

No need.

You seem to be quite sure what you need. Look at this:

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?

Bertram
 
B

Bertram Scharpf

Hi,

Am Freitag, 30. Okt 2009, 22:45:10 +0900 schrieb Aldric Giacomoni:
Bertram said:
My question was not _how_ to implement it but _why_ _not_ to add
it to the interpreter.

[yet another implementation]

Why wouldn't this be implemented by default, er, I don't know, I really
can't think of a time when I'd need this type of behavior.

That's just about _you_ but not about other Ruby programmers.

Bertram
 

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