Fairly simple question I need to take a string containing, for example, “Bob Smith” and return “Bob S.” – or “Javier de Luca” and return “Javier de L.”. In other words, abbreviate the last word in a string to just the first initial and add a period.
Here’s what I have – it works, but it seems clumsy.
str = str.split(' ')
str[str.length - 1] = "#{str.last[0]}."
str = str.join(' ')
Surely, there’s a more elegant way.
This regular expression captures the entire string until the second character of the last word. It then replaces this string with the capture plus a period ala
\1.