Jump to content
Sign in to follow this  
doubleblind

Separating Strings

Recommended Posts

If I have a string of the form "First Second", how do I separate it into two strings composed of each of those words?

Example:

_string = "First Second";

// Whatever I need to make it work

_firstString = "First";
_secondString = "Second";

Any ideas?

Share this post


Link to post
Share on other sites

Pretty easy

_fnc_cutString = {
private "_i";
_i = _this find " ";
if (_i < 0) exitWith {[_this,""]};
[_this select [0,_i],_this select [_i + 1,count _this]]
};


"my string" call _fnc_cutString; // returns: ["my","string"]

Edited by Fight9
  • Like 1

Share this post


Link to post
Share on other sites

Awesome, thanks. I had no idea that command even existed. Can you give me a little insight into exactly how that code works? I want to really understand it so I can adapt it if I need to.

Share this post


Link to post
Share on other sites

sure, I hope this helps

_fnc_cutString = {
private "_i"; // the string will be _this, no need for (_string = _this select 0) or similar
_i = _this find " "; // find will return the index position of " ", -1 if not found
if (_i < 0) exitWith {[_this,""]}; // if -1 (not found), exit fuction with the array [ string, "" ]
[_this select [0,_i],_this select [_i + 1,count _this]] // end the function with array of two strings
// select command can be used to cut string ( string select [start,length] )
// [  string select [0(beginning),index(position of space)] , string selct [index + 1(1 after space), end(total string count)]  ]
};

I'm trying to write a function to cut a string at all the spaces but havent figured it out yet

---------- Post added at 19:34 ---------- Previous post was at 19:30 ----------

BIS already has a function that can do this. You dont even need what I have above

_return = ["my new string"," "] call BIS_fnc_splitString;
// returns ["my","new","string"]

Edited by Fight9

Share this post


Link to post
Share on other sites

Ah... Length of selection, not index position to finish. Good catch, I guess.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×