How do i separate the returned string from string.find

if i use the function string.find inside of a script

string.find("This is a test?:Hello world!:This is another test.", ":")

how would i seperate the string from (number, number, string) ((the thing it returns))

2 Likes
local start, end = string.find(str)

string.sub(str, start, end)
local inp = "This is a test?:Hello world!:This is another test."
--Keep all tuple arguments instead of just the first
local result = {string.find(inp, ":")}
print(result[3]) --print the 3rd one

wait so i can do

local number1,number2,returnedstring = string.find("This is a test?:Hello world!:This is another test.", ":")
-- and will returnedstring be 'Hello world!'?
2 Likes

seems like kind of a hacky way to do it

1 Like

According to documentation string.find only returns the start and end of the operation not a third “returnedstring”. I don’t know who got the idea that it returns a string as well.

That’s more optimal than my reply, my reply is useful when you don’t know the amount of returned arguments.

You can also use the select function:

print(select(3, string.find("This is a test?:Hello world!:This is another test.", ":")))

it says inside of studio that it returns (number?, number?, ... string) shouldnt that mean it returns a string
image

2 Likes

No to be honest I don’t know what it means.

Just do it normally:

local i, j = string.find(str)

local substring = string.sub(str, i, j)
1 Like

It does return a string, for your case it returns an empty one as the third argument. Although not sure what it is.

Also are you trying to split the string?

local inp = "This is a test?:Hello world!:This is another test."
local parts = string.split(inp, ":")
print(parts[2]) --Hello world!
2 Likes

this option works for me the best, thanks

What are you talking about.

print(string.find("This is a test?:Hello world!:This is another test.", "Hello world"))

it only prints (17, 27)


Just do it normally. It works exactly as intended.

local str = "This is a test?:Hello world!:This is another test."
local i, j = string.find(str, "Hello world")
local subString = string.sub(str, i, j)

print(subString)

EDIT: nvm it seems you were trying to split the string this whole time

It prints, 17, 27, "", but you can’t visually see empty strings.

There is no third argument or blank string that gets returned.

string.find() returns two numbers. Where the substring starts and where it ends within the string.

if you want to get the substring you use string.sub(str, start, end) (you get start and end from string.find)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.