- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to iterate individual characters in a Lua string?
A string in Lua is a sequence of characters that we can iterate over in different ways.
After Lua 5.1, we have many approaches which we can use to iterate over the characters that made a string, and we can even do anything we want with them, like make use of them in another example, or simply print them.
Let’s consider the first and the most basic approach of printing the individual characters of a string.
Example
Consider the example shown below −
str = "tutorialspoint" for i = 1, #str do local c = str:sub(i,i) print(c) end
In the above example, we used the famous string.sub() function, that takes two arguments, and these two arguments are the starting index of the substring we want and the ending index of the string we want. If we pass the same index, then we simply need a particular character.
Output
t u t o r i a l s p o i n t
A slightly faster approach would be to make use of the string.gmatch() function.
Example
Consider the example shown below −
str = "tutorialspoint" for c in str:gmatch"." do print(c) end
Output
t u t o r i a l s p o i n t
- Related Articles
- Iterate over characters of a string in Python
- How to split a string in Lua programming?
- How to convert a string to int in Lua programming?
- How to set the style of individual characters in IText using FabricJS?
- How to shift the baseline of individual characters in IText using FabricJS?
- How to set the style of individual characters in Text using FabricJS?
- How to shift the baseline of individual characters in Text using FabricJS?
- How to Replace characters in a Golang string?
- How to convert JSON string into Lua table?
- How to extract characters from a string in R?
- How to Remove Characters from a String in Arduino?
- How to remove certain characters from a string in C++?
- How to remove specific characters from a string in Python?
- How to scan a string for specific characters in Python?
- How to count the number characters in a Java string?
