How do I split a string by a separator, preferably whitespace, and put it into an array? [SOLVED]

0 favourites
  • 3 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I am looking for a way to split a string by whitespace separator and put it into an array in construct.

    I would like to be able to split user's string input into an array. Separator must take into account whitespace " ".

    There is a split function in javascript that does it right of the bat.

    This is partly what i would need

    var str = "How are you doing today?";

    var res = str.split(" ");



    But still i would need it in an array [how, are, you, doing, today?] == this is my desired output.

    I have found a way the split function is built here:

    function CustomSplit(str, delimiter, removeEmptyItems) {
     if (!delimiter || delimiter.length === 0) return [str];
     if (!str || str.length === 0) return [];
     var result = [];
     var j = 0;
     var lastStart = 0;
     for (var i=0;i<=str.length;) {
     if (i == str.length || str.substr(i,delimiter.length) == delimiter)
     if (!removeEmptyItems || lastStart != i)
     result[j++] = str.substr(lastStart, i-lastStart);
     lastStart = i+delimiter.length;
     i += delimiter.length; 
     } else i++;
     return result;

    alert(JSON.stringify(CustomSplit("how do you do", ",", true)));

    alert(JSON.stringify(CustomSplit("c$?as$?$?a$cc$?", "$?", true)));

    the code to run is here:

    found it here

    Unfortunately it is not granular enough, because it uses a different function, namely:


    I got stuck looking for a way to build a granular str.substr().

    However, maybe there is an easy way to split a string by whitespace separator and put it into an array in construct?

    Please help!

  • See tokenat and tokencount expressions:

    Repeat (tokencount(text, " ")-1) times
    	Array set at loopindex value: tokenat(text, loopindex, " ")
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • dop2000> I love Construct 3!, you are ingenious and your suggestion is remarkable.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)