Pregunta

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

  2. Why is gsub used twice in above code? Both the times, the letters are being converted to upper case! Please explain what's exactly happening in above line of code.

¿Fue útil?

Solución

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

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

    For instance, a string "hello world" turns into "Hello world" after the first string.gsub(), then turns into "Hello World" after the second string.gsub().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top