Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do you get the tail end of a string?

Reply
Thread Tools

How do you get the tail end of a string?

 
 
Just Another Victim of the Ambient Morality
Guest
Posts: n/a
 
      10-30-2009
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!


 
Reply With Quote
 
 
 
 
Michael W. Ryder
Guest
Posts: n/a
 
      10-30-2009
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]?
 
Reply With Quote
 
 
 
 
Michael W. Ryder
Guest
Posts: n/a
 
      10-30-2009
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. 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].
 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      10-30-2009
> 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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      10-30-2009
7stud -- 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!

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

 
Reply With Quote
 
Rajinder Yadav
Guest
Posts: n/a
 
      10-30-2009
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"]

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.

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      10-30-2009
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/]

Bertram


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

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      10-30-2009
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

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...es/String.html

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

Bertram


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

 
Reply With Quote
 
Albert Schlef
Guest
Posts: n/a
 
      10-30-2009
Bertram Scharpf wrote:
> 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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Thomas Preymesser
Guest
Posts: n/a
 
      10-30-2009
[Note: parts of this message were removed to make it a legal post.]

2009/10/30 Just Another Victim of the Ambient Morality <
http://www.velocityreviews.com/forums/(E-Mail 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 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."

 
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
Tail Call Optimization (Tail Recursion) Terry Michaels Ruby 16 04-20-2011 11:37 AM
Best method for seeking to n lines before end of file [ implementation of tail ] graemenewlands@gmail.com Perl Misc 6 06-01-2006 03:44 PM
Get the tail of a PriorityQueue? jimi_usenet@hotmail.com Java 4 02-16-2006 03:09 PM
How to delete the node at the end (tail) of the Linked List ? Sumika C Programming 4 07-15-2005 07:14 AM



Advertisments