string - Use of ^ and gsub in Lua -


function title case(thestring)       return (thestring:gsub("^%a", string.upper):gsub("%s+%a", string.upper))  end 
  1. i have above mentioned code. wanted know use of ^ operator in above code. know ^ used in sets exclude/negate succeeding values in set since it's not set, ^ meant ?

  2. why gsub used twice in above code? both times, letters being converted upper case! please explain what's happening in above line of code.

  1. ^ in beginning of pattern anchor "the beginning of string", i.e, "^%a" matches "%a"(a letter) if it's in beginning of string.

  2. the first string.gsub() make first letter in string uppercase, second string.gsub() make first letter of other words (a letter follows whitespace) uppercase.

    for instance, string "hello world" turns "hello world" after first string.gsub(), turns "hello world" after second string.gsub().


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -