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

How can I split a string into 2 separate strings?

Asked by
Teeter11 281 Moderation Voter
8 years ago

How can I take one string and separate it into 2 separate strings?

I don't even know what to say down here.

3
How are you going to decide how to break them up? Is it, by comma? Is it by a phrase? Is it by a number of letters? BlueTaslem 18071 — 8y
0
Lets say i was saving data and I put it into a string "PlayerName:2" how could I split it in half at the : ? Teeter11 281 — 8y

1 answer

Log in to vote
3
Answered by 8 years ago

This is very simple, and is seen in almost every script. :)

The Characters are the ..; They allow the use of separated strings.

What do you mean? Your not making sense

As I was afraid, although, I'll give an example of what I mean (hopefully that'll clarify more):

local String = "Some String"

As you can see, the Variable 'String' is containing a sequence of Letters (a string consists a sequence of Letters, Number, Symbols, ect.), but, we can separate the two;

local String = "Some " .. "String"

And, when you check the Output, you'll notice that it prints (if you use the 'print' function for this) 'Some String' in the Output window. :)

But this key also has more advantages; You can join together other Strings into one, let me show you what I mean:

local String1 = "Some "
local String2 = "Random String"
local String3 = String1 .. String2 --Note: Keep the Variables/String separate from the '..'; Makes the code look a bit cleaner :)

--Or, you can do

local String1 = "Random String"
String1 = "Some " .. String1 --Same thing as the code from before, but only a bit messier

Other than strings, you can also do Numbers (however, I don't recommend this) with this, and it'll work just the same:

local String1 = "Number: "
local Number1 = 1
local FinalString = String1 .. tostring(Number1) --Why did I use 'tostring'? Without it, it just looks super messy to me :)

Information left out

It is not such a good idea to keep the Strings with the '..' so closed, by that I mean like this, '"String ".."String"', it looks super messy unless you place it out. :)

I also do not really recommend doing Numbers when using these advantages, as it may cause errors, but, if you do, I highly recommend using tostring.

  1. tostring - Basically, a Function in Lua (the language) that converts a Number to a String, or as the WIKI says; 'Receives an argument of any type and converts it to a string in a reasonable format.'

  2. Strings - Are sequences of Numbers, Letters, Symbols, ect.

Hope this helped!

-- EDIT --

Lol, thanks for the Feedback. :) Now I have a full understanding of the problem.

It would require you to use the :sub method of a String; :sub (or it's full name, Sub-String) is used to get the position within a String (Start to End).

You lost me, what do you mean exactly?

Oh, well, sorry then, I don't mean to make this so confusing. :( But, the answer is simple enough. :)

Let me show you of what I mean (Lol, I was super tired at the time, so this new edited answer may be choppy. XD );

local String = "PlayerName:2" --The Variable 'String' is saving 'PlayerName:2' as a String
local String1  = string.sub(msg,1,11)
local String2 = string.sub(msg,12)

Now, when you print the results in the Output, you will see that they are, in fact, separated into two separate strings (I guess :P).

Sub-String - The Sub-String (in lua) is function that return the positions of a sequence of Letters, Symbols, Numbers between the two arguments (The Second and Third; Start and End Arguments), however, if the Third is absent, then it will return the starting point of the String still, until the end of the string, or, as the WIKI says;

'Returns the substring of s that starts at i and continues until j; i and j may be negative. If j is absent, then it is assumed to be equal to the length of s. In particular, the call string.sub(s,1,j) returns s until as many characters as j, and string.sub(s, -i) returns a suffix of s with length i.'

Hope this helped, and sorry if this was a very messy and choppy answer. :)

0
While this is a good explanation, he's clearly asking about the other way. Breaking one string into two. BlueTaslem 18071 — 8y
0
@BlueTalsem Oh, ok, thanks for letting me know. :) I'll try and come up with another answer, then edit my post ASAP. :) TheeDeathCaster 2368 — 8y
0
Although I do like when answered are as detailed as this It's not what I am looking for but it is kind of my fault for not explaining it correctly. I do mean what bluetaslem said. Teeter11 281 — 8y
0
@BestDeveloper Edited my Answer. :P TheeDeathCaster 2368 — 8y
Ad

Answer this question