doubleblind 13 Posted May 2, 2015 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
fight9 14 Posted May 2, 2015 (edited) 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 May 2, 2015 by Fight9 1 Share this post Link to post Share on other sites
doubleblind 13 Posted May 2, 2015 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
fight9 14 Posted May 2, 2015 (edited) 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 May 2, 2015 by Fight9 Share this post Link to post Share on other sites
killzone_kid 1331 Posted May 2, 2015 // select command can be used to cut string ( string select [start,finish] ) start yes, finish no https://community.bistudio.com/wiki/select Share this post Link to post Share on other sites
fight9 14 Posted May 2, 2015 Ah... Length of selection, not index position to finish. Good catch, I guess. Share this post Link to post Share on other sites