0% found this document useful (0 votes)
19 views3 pages

String Find

Code explaining String.find() function in lua using roblox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

String Find

Code explaining String.find() function in lua using roblox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

local text = "The quick brown fox jumps over the lazy dog"

local start, finish = string.find(text, "fox")


print("Found 'fox' at indices:", start, finish)

--This example is to explain functions in a table


local String = {}

String.Calc = function(a , b)
return a + b, a - b , a * b , a / b
end

add, sub , mult, div = String.Calc(3, 13)

print(add, sub , mult, div)

-- Example of string formating and use of string.sub()

local sentence = "The lazy cat is lying on the couch"


local start, finish = string.find(sentence, "lazy")
print("\n\nFound word starting with \"lazy\":", "\n\nString : "
.."\""..sentence.."\"".."\n\nWord Found :\n\n".."\""
..string.sub(sentence, start, finish).."\""..
"\n\nPositions :\n\nStart Position : "..start..
"\n\nEnd Position : "..finish.."\n\n")

print(start, finish, " This is an example of , ")


print(tonumber(start..finish) == 58)
print(start.." "..finish)
print(start.." , "..finish)
print("what is this called :\"\"")

-- example for multiple occurance of an given word


local story = "The quick brown fox, the sly fox, and the cunning fox."
local pattern = "fox"
local start, finish = string.find(story, pattern)
local counter = 0

while start do
print("\n\nFound 'fox' at indices:", start, finish.."\n\n")
start, finish = string.find(story, pattern, finish)
counter = counter + 1
if start == nil then
local start, finish = string.find(story, pattern)
print("\n\nUsing sting.sub() to print the word : "
.."\""..string.sub(story, start, finish)
.."\"\n\nNumber of repetations : "..counter)
end

end

-- example to matych an exact pattern

local Ticket = "DWRE21-E42FTR-W@$%rT"


local pattern = "[%u][%p][%p][%p][%l][%u]"
local age = 19

local start, finish = string.find(Ticket, pattern)

if start and finish then


print("\n\n"..string.sub(Ticket, start, finish).."\n\n I.D passed verification")
if age >= 18 then
print("Welcome to the V.I.P Section, drinks are free!")
else
print("Welcome to the Family V.I.P section, Soda Drinks are free!")
end
else
print("Alert!!! Ticket verification Failed")
end

-- example to extract domain from a url

local function extractDomain(url)


local startPos, endPos = string.find(url, "://([^/]+)")
if startPos then
local domain = string.sub(url, startPos + 3, endPos)
return domain
else
return "Invalid URL: " .. url
end
end

print(extractDomain("https://www.outlook.com"))
print(extractDomain("http://sub.example.who.org/pages"))
print(extractDomain("http:///sub.example.who.org/pages"))

-- example to extract an email address from url


local text = "Contact us at support@example.com or info@company.org"
local pattern = "[%w%.]+@[%w%.]+"
local startPos, endPos = 1, 1

while startPos do
startPos, endPos = text:find(pattern, endPos)
if startPos then
local email = text:sub(startPos, endPos)
print("Found email:", email)
endPos = endPos + 1
end
end

-- example to extract phone numbers from an string

local text = "Call us at (123) 456-7890 or 9876543210"


local pattern1 = "%(%d%d%d%) %d%d%d%-%d%d%d%d"
local start1, finish1 = text:find(pattern1)
if start1 then
local phone = text:sub(start1, finish1)
print("Found phone number:", phone)
else
print("No phone number found.")
end

local pattern2 = "%d%d%d%d%d%d%d%d%d"


local start2, finish2 = text:find(pattern2)
if start2 then
local phone = text:sub(start2, finish2)
print("Found phone number:", phone)
else
print("No phone number found.")
end

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy