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?
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.
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
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)
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
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.