這個string.byte()function 是最廣泛使用的 Lua 字符串庫函數之一,它接受一個字符或一個字符串作為參數,然後將該字符轉換為其內部數字表示。
字符到內部數字表示可以很容易地從 ASCII 表中解釋。
用法
string.byte(ch) or string.byte(ch,idx)
在上麵的表示中string.byte()函數,chidentifier 表示我們要轉換為十進製值的字符。此外,該idx標識符表示作為參數傳遞的字符串索引處的字符。
讓我們考慮幾個使用 string.byte() 函數的示例。
示例
Consider an example shown below −
s = string.byte("a")
print(s)
輸出
97
我們還可以將參數中的字符串傳遞給string.byte()函數,如果我們不指定任何索引,那麽該字符串將隻打印第一個字符的內部整數表示。
示例
Consider the example shown below −
s = string.byte("abc")
print(s)
輸出
97
示例
讓我們考慮一個例子,我們將一個索引作為第二個參數傳遞給string.byte()函數。考慮下麵顯示的例子 -
s = string.byte("abc",2)
print(s)
輸出
98
注意− 字符串的索引從 1 開始。
相關用法
- Lua string.char()用法及代碼示例
- Lua string.gsub()用法及代碼示例
- Lua string.lower()用法及代碼示例
- Lua string.format()用法及代碼示例
- Lua string.upper()用法及代碼示例
- Lua table.pack()用法及代碼示例
- Lua io.popen()用法及代碼示例
- Lua table.unpack()用法及代碼示例
- Lua math.modf()用法及代碼示例
- Lodash _.isValidDate()用法及代碼示例
- Lodash _.isInteger()用法及代碼示例
- Lodash _.sampleSize()用法及代碼示例
- Lodash _.fromQuery()用法及代碼示例
- Lodash _.noConflict()用法及代碼示例
- Lodash _.Intersection()用法及代碼示例
- Lodash _.values()用法及代碼示例
- Less isstring()用法及代碼示例
- Lodash _.isLength()用法及代碼示例
- Lodash _.third()用法及代碼示例
- Lodash _.negate()用法及代碼示例
注:本文由純淨天空篩選整理自Mukul Latiyan大神的英文原創作品 string.byte() function in Lua programming。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。