How do I get index of character from string

Does anybody know how I can find what letter of a string it is?
Ex:

local word = "abc"
if letter "c" of word == 3 then
   true
end

try this script

local function FindIndex(Word:string, Letter:string)
	for i = 1, #Word, 1 do
		if string.sub(Word, i, i) == Letter then
			return i
		end
	end
end
1 Like

It says number expected, im not really sure why its returning anything else. Do I need to make it so it translates to a num from string?

Try changing the #Word to string.len(Word).

1 Like

ok I will do that. That makes more sense since #string is meant for a count of objects I think

Well, @kittyPGR Isnt Correct, but its on the Right track, you can get a specific character by using string.sub, however if you want to check if the number is valid, you can do this:

local sign = math.sign -- the method I used to check if a number wasnt equal to an amount

function IsCharacter(str, char, pos)
     -- sign returns a number between -1 and 1 using the size of the string
    assert(sign(#str) > -1, "pos is less than the valid amount") -- this is optional, so is the above

    local character = string.sub(str, pos, (pos - #str) - 1) -- gets the Character from the Position provided
    -- 2 - 5 = -3 - 1 = -4, -4 would be our opposite value to our position, without subtracting one, we would get two values instead of one
    return (character == char) -- true or false value
end

print(IsCharacter("Hello", "e", 2)) -- this should return true

str is the string you want to check
char is the character you want to look for
pos is the area within the string you want to look into

Obviously, this doesnt work for more than 2 characters, but it you might be able to do more checks

Edit: @kittyPGR would be corrext, just not very efficient.

2 Likes

Why would you change # for a more inefficient version of it?

1 Like

which line are you getting the error ? cus it’s running fine for me
image

1 Like

your script verifies if the letter is in the given position of the string where as my script finds the position of the letter in the string… so ig it’s not wrong, just doing a different task.

1 Like

ya dont say…

You would be better off using string.find tho:

local _1, _2 = string.find(str, pattern)


print(_1, _2) -- gives you the location of the character
1 Like
local function FindIndex(Word, Letter)
	for i = 1, #Word, 1 do
		if string.sub(Word, i, i) == Letter then
			return i
		end
	end
end

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

This should work fine but I am getting at if string.sub(FindIndex(text, v) - 1) I’m probably doing this wrong though

So I have searched this up so many times and tried searching it in so many ways, and theres a straight forward code from lua? This is genuinely frustrating. Thank you

you are even using it within your code?

Also, If you want to create a more better version of it, you may be able to us @kittyPGR’s code as string isnt exactly the best with certain things, and thats probably why the utf8 library exists.

So I should implement string.find? And yes this is in my code. What I want to detect if there is a space behind and before the character u. Signifying that it means you. This avoids my program finding the letter u inside words and then have trouble finding who you are talking about.

According to my results after benchmarking, you should use string.find, it is overall faster with its results than @kittyPGR 's FindIndex

Code to Test Results:
local function FindIndex(Word, Letter)
	for i = 1, #Word do
		if string.sub(Word, i, i) == Letter then
			return i
		end
	end
end

local _1 = "hello" -- string to use
local _2 = "e" -- character to look for



local a -- placeholder
local t = os.clock() -- the start time
for i = 1,1e4 do -- we run this 10,000 times so our data is accurate
	a = FindIndex(_1, _2)
end
local e = os.clock() - t -- elapsed time since the code fired
print(`{a}: {e}: function find`) -- printing of results in output


----------------------------------------------
-- string.find

local a
local t = os.clock()
for i = 1,1e4 do
	a = string.find(_1, _2)
end
local e = os.clock() - t
print(`{a}: {e}: string.find`)

not really sure.

2 Likes

This is what I tried, so I will use string.find in the function.

if string.sub(FindIndex(text, v) - 1) == " " and string.sub(FindIndex(text, v) + 1) == " " then

I notice in your example for string.find. It has two values in local pattern. “_1, _2” I do not understand how this works, are you able to explain?

The first Argument is looking for the string, that the numbers, also I think I figured it out:

local str = "hello" -- test string
local character = "l" -- test character

local i = string.find(str, character) -- the number for the character

local subi = (i - 1) -- subtracts the value from the index to get the character behind

if subi > 0 then -- if there is a character behind the value (greater than 0)
    print(string.sub(str, subi, subi)) -- the character behind the first character
end

string.find returns two arguments, thats why I used two variables, if I used one, it will only get 1 of the values

you dont usually do this yourself, but you can:

function ex()
    return 5, 10 -- returns two values
end

local x, y = ex() -- used two variables to retrive the Data

print(x - y, x + y) -- -5, 15

1 Like

I thought of string.find at first but not sure why I used string.sub. Ya string.find is better than string.sub for this case.

1 Like

It tells me to give me a number for string.sub. Maybe this is because I have done this with a local function. Quite a strange occurrence, however do you think doing the whole script in a function will detect if its in-between the spaces?

Are you even using it correctly?

It would detect the spaces as a character because in the software, its considered a character, but you can use string.gsub to replace them with nothing at all.

1 Like