Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created December 4, 2021 05:07
Show Gist options
  • Save rozer007/f3538e2049ace187912f532117557dcb to your computer and use it in GitHub Desktop.
Save rozer007/f3538e2049ace187912f532117557dcb to your computer and use it in GitHub Desktop.
Strings in lua
Q1) Can't we use the + operator to concatenate two strings?
Ans: No ,In lua to concatenate two strings we should .. operator. eg. st1..st2.
Q2) How can we find the length of a string without using the string length function ?
ans: We can use # symbol in front of the string to find the length of the string.
Q3) Can we access the character of a string using thier indexes?
ans: No we can't and if we try to access it will nil value.
Q4) what is gmatch function in string?
ans: This function give the string which matches a certain pattern from a original string.
Q1. what will be the output of this statement:
ts="pep"
st="coder"
print (ts..#st)
a) pepcoder
b) error
c) pep#
d) pep5
ans. d)
Q2. What will be the output fo the given statement:
a="hello pepcoder"
for b in string.gmatch(a, "[^%s]+") do
print(b)
end
a) hello pepcoder
b) _________
c) hello
pepcoder
d) none of the above
ans: c)
Q3.What will be the output:
a="hello pepcode"
io.write(string.gsub(a,"o","Op"))
a) hellOp pepcOpde
b) hellOp pepcOpde2
c) hellop pepcopde
d) none of the above
ans: b)
Q4. What will correct output for this statement:
b="12.122344443"
print(string.format("%.5f",b))
a) 12.122344443
b) error
c) 12.12234
d) none of the above
ans. c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment