官术网_书友最值得收藏!

String length

There are two ways to get the length of a string, either using the string.len() function, or by placing a # symbol in front of the string. Both methods work the same way, and they both return a number value. This number can be assigned to a variable or used in its place. You can call either method on a variable, or directly on a string. The following code demonstrates all of these concepts:

hello = "hello, world"
-- Assign length to variables
count_hash = #hello;
count_func = string.len(hello)
print ("The string:")
print (hello)
-- Print the variables assigned at the top
print ("Has a length of:")
print (count_hash)
print(count_func)
-- Use string literals, in place
print (#"hello, world")
print (string.len("hello, world"))
主站蜘蛛池模板: 涪陵区| 永顺县| 垫江县| 常山县| 芜湖市| 溧阳市| 六安市| 虎林市| 瓮安县| 广昌县| 洱源县| 刚察县| 汕头市| 巴林左旗| 且末县| 民乐县| 黄山市| 凤冈县| 太仆寺旗| 洮南市| 襄城县| 阿图什市| 法库县| 建阳市| 万州区| 古田县| 秦皇岛市| 钦州市| 长顺县| 桂林市| 乌鲁木齐市| 乐平市| 和平区| 宜阳县| 海阳市| 扎兰屯市| 嘉义市| 沙湾县| 毕节市| 盈江县| 浪卡子县|