Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Searching for a substring

Sometimes, you may have a large string (such as the contents of a file) and need to find out if it contains a smaller substring. This can be done with the string.substring function. This function takes two variables: the large string to search and a smaller string to look for. On success, it returns a number, which is the index at which the substring first appears. On failure, the function returns nil:

local sentence = "The quick brown fox"local word = "quick"local index = string.find(sentence, word)print ("substring found at index: " .. index)
The second argument is interesting; it doesn't just have to be a string. The second argument to string.substring can be a pattern, which results in regex like searching. For ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content