Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I divide a string in parts?

Asked by
Tesouro 407 Moderation Voter
9 years ago

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?

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) BlueTaslem 18071 — 9y
0
well, there is no TableValue object, then what I got left is strings Tesouro 407 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

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)
Ad
Log in to vote
0
Answered by
Discern 1007 Moderation Voter
9 years ago

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

Answer this question