Split String at Index?

I’m basically wondering if I could do something like this:

string.split(string,index)

This doesn’t work but it doesn’t error, it just doesn’t split anything. Does anyone know something like this that would work? Or do I need to use a different function like string.gsub() or something?

can you send me the whole script

Sure

local function TranslateText(text)
	local words = text:split(" ")
	local translatedText = ""
	
	for i, word in ipairs(words) do
		local firstVowel = FindFirstVowel(word)
		word = word:split(firstVowel)
		print(word)
	end
	
	return translatedText
end

This is the part of my script that I need to use it for. FindFirstVowel will return a number or nil.

what is word:split() ?

char limit

That’s what I need help with because it doesn’t work.

you must use it like this:

word.split(word, firstVovel)

But I used a colon instead so that shouldn’t be the problem. You can use it like this as well:

string:split(pattern)
1 Like

i never used it like that, maybe it works but like i said i never used that, i always use

word.split(word, firstVowel)

so your code must like this,


local function TranslateText(text)
	local words = text.split(text, " ")
	local translatedText = ""
	
	for i, word in ipairs(words) do
		local firstVowel = FindFirstVowel(word)
		word = word.split(word, firstVowel)
		print(word)
	end
	
	return translatedText
end

but this code will return nil because you return Translated Text but you did not set anything for it

Yes because right now I’m just printing it so it should work in the console.

Also, apart from you replacing my method with yours, this isn’t any different.

did it work ?

char limit char limit

It didn’t, no.

If I read correctly from your post, you want to split the string at a specific index. e.g:

"ThisIsAString"

the letter would be “i” in the word “Is” at index 5

The index you would want (let’s say 5)

You could try to split the entire string into a table, for every index being a letter
then restructure the string from those letters in the table such like:

{“T”,“h”,“i”,“s”,“I”,“s”} (and so on in the table for every letter, cba to type it)

then loop through the table like such:

local newString = ""
for index, letter in pairs(tableWithLetters)
    if (index == indexWanted) then
        break
    else
        newString .. letter
    end
end

and the newString variable should be the first split of the entire string.
And if you construct the second half of the table into a string you’ll get the other part of the string.

(Note: this is probably a terrible way of doing it since I know nothing about strings)
(Side note: instead of stringObj:split() I suggest using the string.split() function)

2 Likes

string and vowel should be two strings, then, if your string has like, 4 vowels, then do this:

word.split(string, vowel)[4] -- Getting the fourth splitted word

can you try this and send me console

local function TranslateText(text)

        print("Function Works "..text

	local words = text.split(text, " ")

        print(words)

	local translatedText = ""
	
	for i, word in ipairs(words) do

                print(word)

		local firstVowel = FindFirstVowel(word)
		word = word.split(word, firstVowel)

		print(word)
	end
	
	return translatedText
end

but he must send him a table in console but he says it prints nothing

1 Like

It’s interesting to see some pretty complicated responses here. If I didn’t misunderstand, you can do something that is as simple as using two string.subs.

local function split(s, i)
    return string.sub(s, 1, i), string.sub(s, i + 1, #s)
end

This will return two strings, one has i many characters, the other complements it.

It doesn’t error, because string.split is a function, converting a given string to a table. Every part of the string separated with a given separator is pushed onto the table.

1 Like

Thanks, this takes up much less space I can’t thank you enough. :hugs:

Just one question though, this splits after the index, are you able to split it before?

1 Like