How do I get index of character from string

I still have the same error.
image
If I’m missing the obvious please inform me. Because I am getting tired.

yes, you are probably not separating what each argument is supposed to be, or you arent applying the values and are expecting the function to just do the work for you, can you show the code?

local function FindIndex(Word, Letter)
	local int = string.find(Word, Letter)
	return int
end

	for _, v in pairs(targetai) do
		if string.find(text,v) then
			if v == "u" then
				if string.gsub(FindIndex(text, v) - 1) == " " and string.gsub(FindIndex(text, v) + 1) == " " then
					print("its just u")
				end
			else
				
			end
		end
	end

Sorry I sent the wrong function for a sec

And thats why, you are completely missing the arguments part of the Arguments, the function requires a specific type of value, and you giving what it does not want.

So i should translate it to a different value, via tostring or tonum?

Im not even going to bother explaining because its so simple:

if #v == 1 then
    print(`its just {v}`)
end

at this point I gtg.

1 Like

Ok thank you! I will mark the solution tomorrow when I have more time to read everything.

Sorry to interrupt one year later.

There’s an actually fairly easier way to do this than what other’s said.

By using the string.match you can easily achieve this!

By your request the code would look like this:

local word = "abc"
local letter = string.match(word, "c")

if letter == "c" then
   print('Letter returned: '.. letter)
end

If you wanted to put it in a function it’s rather easy and could look something like this:

local word = "abc"
local letter

function GetLetter(word, letter)
      return string.match(word, tostring(letter)) or nil
end

letter = GetLetter(word, "c")
print(letter)

While these are simple examples. there’s another way to make sure you’re getting the right word! by means only one in the string.

Otherwise you can use the simple string.match without any modifications

This example is what you requested, the other people what said take example DasKairo gave you a long code for this, but fairly right.

Using string.find() is only useful when you want to get the location of the column in a string
{Column number}

local _1, _2 = string.find("This is 10", "0")
print(_1, _2)

In the script above me says the number ‘0’ is at column 10 while there are only 8 letters?
Space also counts as a column!

You can pair this with stuff like string.sub which is a great way to implement this, but the most efficient and short way is by using string.match

1 Like

:thinking:

Because a space is also a character in unicode, and takes up bytes like any other character.

U+0020 | ASCII 32