string concat person's name

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,

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

Or,

‘first middle last_name’.split.last

Jamey

Thanks Stephan! I found out that this also works:

array = name.split(' ') array[array.length-1].to_s