String Library Tutorial. You can find details about the string library in section 5.4 of the Reference Manual (Lua 5.1) 1 and in section 6.4 of the Reference Manual (Lua 5.3) 2. For practical examples of usage of the string library, have a look at StringRecipes. Note: In Lua string indices start at index value 1 (as they do in C), not. Lua is an open source language built on top of C programming language. Lua has its value across multiple platforms ranging from large server systems to small mobile applications. This tutorial covers various topics ranging from the basics of Lua to its scope in various applications. String.upper(argument) Returns a capitalized representation of the argument. 2: string.lower(argument) Returns a lower case representation of the argument. 3: string.gsub(mainString,findString,replaceString) Returns a string by replacing occurrences of findString with replaceString. 4: string.find(mainString,findString, optionalStartIndex,optionalEndIndex).
Easy Written By ForeverKyIe
Learn how to use 'tonumber()' and 'tostring()'.
'tostring()'
The function 'tostring' is used to convert a numbervalue to a stringvalue.
For example:
Let's say that the variable 'value' is identifying a NumberValue in theworkspace, and the variiable 'part' is finding a 'Part' in the workspace.
How To Do String In Lua Cho
The 'tostring()' will allow me to change the value of the numbervalue to astringvalue for naming my part.
'tonumber()'
The function 'tonumber' is very similar to the 'tostring()' function.
Tonumber performs the same function, except, it can change a string valueto a number value.
For example:
Let's say that the variable 'text' is finding a stringvalue under the workspace,and the variable 'numberValue' is finding a NumberValue under the workspace.
With the 'tonumber()' function, I can convert the string value for the variable'text' to a number value.
Hopefully this will help you a little bit while learning LUA.
Easy Written By ForeverKyIe
See ForeverKyIe's profile on Roblox
|
There are many ways to implement the 'trim' function [1] in Lua:
Optional C module for test15 based on LuaList:2009-12/msg00951.html:
Test results (lower numbers = faster):
The speed of trim5
is relatively favorable or competitive over the data set except it is quite poor in the case of a long string with only whitespace. The use of '.*
' (rather than a non-greedy '.-
') quickly plows to the end of a long string, and the '%S
' then triggers backtracking to avoid the trailing whitespace, but the juxtaposition of '%s*
' and '.*
' imposes substantial backtracking (O(N^2)) if no '%S
' match is found. trim6
handles the worst cast condition specially and behaves well over the entire data set. trim7
is a variant that localizes functions, which gives a bit larger code size and does not give a substantial speed improvement. (On further testing, each localization gives maybe 10% in best case involving empty string data and inlining the function call, but here there are two calls to match
). trim11
is also a variant of trim6
with similar performance characteristics, perhaps slightly better, except speed is about half in the case of a long string with only whitespace (this is fixed in trim12
). trim13
(or its variant trim14
) is an lpeg (LuaPeg) implementation, which generally meets or exceeds the performance of the best pattern matching implementations, particularly exceeding in the case of lots of whitespace, but it is a bit slower in its worst case condition of alternating space and whitespace. The C implementation (trim15
) is by far the fastest over the entire data set.
Reusing the same strings across all test iterations, as done above, might not properly gauge the effect of temporary string creation due to Lua's string interning. A quick test with local ss = {}; for i = 1,80000 do ss[i] = ' ' .. (' '..i):rep(10) .. ' ' end
as a string cache doesn't seem to affect the basic conclusions above.
--DavidManura
See Also
- LuaList:2009-12/msg00877.html - related discussion