Вопрос

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.

Это было полезно?

Решение

  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().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top