Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I divide a string in parts?

My question didn't explain it correctly, sorry. I wanna divide a string in parts by placing special characters between them, for example:

I have this string: Blue

and this string: Square

but I can't have more than a string value, so I wanna join them and separate later, using some kind of character to spot the divisions, like: #Blue#Square

or different characters: #Blue@Square

So when I divide it, all the text between # and @ will be a string, and all the text after @ will be other. But I have no idea how to do it, help?

2 comments

0

As a general comment -- if you're working with them in the understanding they are made of several parts, it's probably better to just use a table to store them and combine them only when that's necessary (and when combination and theirs are both necessary, pass both, though this should almost never happen)

0

well, there is no TableValue object, then what I got left is strings

2 answers

In this case, I would recommend string manipulation. It would most likely be something like this:

Execute a for loop with #string.
Check every character with string.char(charnum)
If string.Char(charnum) is # or @, make string equal to string:sub(1,charnum)

What you want to do is set a variable as an indicator for the script (In this case, #). Then use a for loop to loop through every single character in the string. Once you do that, use string.sub() to set 2 variables; 1 for each string. Take a look.

indicator = "#" --What you want to use to seperate the strings.
stringvalue = script.Parent.StringValue --The StringValue that holds the string.

for i = 1, string.len(stringvalue) do --Creates the for loop. Just in case you didn't know, string.len returns the amount of characters in a string.
	if string.sub(stringvalue.Value, i, i) == indicator then --Checks if the character is the same as the indicator we set on Line 1.
		indicatorposition = i --"i" is where the indicator is in the string.
		break --Break the for loop so the script knows we found our indicator.
	end --End the if statement.
end --End the for loop.

string1 = string.sub(stringvalue, 1, indicatorposition - 1) --Use string.sub to get every character before the indicator position.
string2 = string.sub(stringvalue, indicatorposition + 1)  --Use string.sub to get every character after the indicator position.

Hope this helped. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.