lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hu Zhe wrote:

> However, it seems still not a look-around. It eats up the
> " " between words. 

Try this:

function Capitalize(Str)
  return (string.gsub(Str, "(%W*)(%w)(%w*)(%W)",
    function(LeadSpace, FirstLetter, Rest, TrailSpace)
      return LeadSpace .. string.upper(FirstLetter) .. Rest
        .. TrailSpace
    end)) -- function
end -- Capitalize

It grabs leading whitespace (if any), the first letter of a
word, the rest of the letters in the word (if any), and
trailing whitespace (if any), and returns exactly what it
grabbed except with the first letter capitalized.