Hi, If I have person''s name and I want to concatenate last name only, how do I do that?(They are separated by space.) ex)first_name last_name ex)first_name middle_name last_name thanks, --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Kim wrote:> Hi, > > If I have person''s name and I want to concatenate last name only, how > do I do that?(They are separated by space.) > > ex)first_name last_name > ex)first_name middle_name last_nameUse regular expressions: # irb>> a = ''first middle last''=> "first middle last_name">> a =~ / *([^ ]*)$/=> 12>> $1=> "last_name" Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Or, ''first middle last_name''.split.last Jamey On Jan 4, 2008 5:27 PM, Stephan Wehner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Daniel Kim wrote: > > Hi, > > > > If I have person''s name and I want to concatenate last name only, how > > do I do that?(They are separated by space.) > > > > ex)first_name last_name > > ex)first_name middle_name last_name > > Use regular expressions: > > # irb > >> a = ''first middle last'' > => "first middle last_name" > >> a =~ / *([^ ]*)$/ > => 12 > >> $1 > => "last_name" > > Stephan > -- > Posted via http://www.ruby-forum.com/. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Stephan!
I found out that this also works:
array = name.split('' '')
array[array.length-1].to_s
On Jan 4, 5:27 pm, Stephan Wehner
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Daniel Kim wrote:
> > Hi,
>
> > If I have person''s name and I want to concatenate last name
only, how
> > do I do that?(They are separated by space.)
>
> > ex)first_name last_name
> > ex)first_name middle_name last_name
>
> Use regular expressions:
>
> # irb>> a = ''first middle last''
>
> => "first middle last_name">> a =~ / *([^ ]*)$/
> => 12
> >> $1
>
> => "last_name"
>
> Stephan
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---