Search Unity

String.split in javascript

Discussion in 'Wish List' started by dacloo, May 14, 2007.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    String.split is a very common used function, and only available in C#. It would be nice to have this function working in javascript.
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    It's called Split and is an object method on string. Everything in the .Net framework is available in all .Net languages including unity JavaScript. The following should work fine in JavaScript:
    Code (csharp):
    1.  
    2. var combined = "one,two,three";
    3. var words = combined.Split(","[0]);
    4.  
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    w00t...I've been discussing this earlier in a post,
    thank you!

    Now my official wish is that this should be included in the Unity docs :)
     
  4. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Dacloo: So sorry I forgot to post this to you today. Completely crossed my mind! :(
    Seon.
     
  5. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    A lot of intrinsic functionality from JavaScript is implemented via .NET. It would be nice if there were a summary of all this somewhere, as it often perplexes me (and thank you Freyr for being so prompt in answering my questions -- I just wish I didn't have to ask them all the time...)
     
  6. wedstrom

    wedstrom

    Joined:
    Aug 30, 2010
    Posts:
    11
    var inputA = input.Split("");

    Error:
    MissingMethodException: Method not found: 'System.String.Split'.
    Boo.Lang.Runtime.MethodDispatcherFactory.ProduceExtensionDispatcher ()
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Read the second post in this thread.

    --Eric
     
  8. wedstrom

    wedstrom

    Joined:
    Aug 30, 2010
    Posts:
    11
    I misread the format in his post.
    When i tried his method before posting, I tried
    var inputA = input.Split("",[0]);
    wonderin what the hell [0] was supposed to do. It looks like a reg exp.
    It doesn't work that way.

    After a closer examination, I noticed that [0] was passing the first element in the string ","[0], and I retyped. It works now.

    What had me confused was that rather than telling me my parameters were incoreect, the engine said that Split does not exist as a method of the string class, which pointed me in the wrong direction.
     
  9. wedstrom

    wedstrom

    Joined:
    Aug 30, 2010
    Posts:
    11
    Did I mention that I hate how "Native" Javascript functions don't have a proper reference?(If such a reference exists, please correct me) I mean come on, in real JS the first element of "a" === "a".
     
  10. DarkSlash

    DarkSlash

    Joined:
    Sep 30, 2011
    Posts:
    127
    I got he same issue (MissingMethodException: Method not found) but I don't see the solution on the second post! :(
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The code in the second post works fine and doesn't produce any errors. Take another look.

    --Eric
     
  12. gooncorp

    gooncorp

    Joined:
    Dec 30, 2011
    Posts:
    131
    doesnt work for me. i get an error that says "System.String[]".
    with my debug.print i should get the letter "w".
    i know this is pretty basic im just having a hard time to get it to work properly.
    i am using this code :

    #pragma strict

    var string1 : String = "w,o,r,d";


    function Start () {


    // InvokeRepeating("narrativeFirst", 1, 1);

    narrativeFirst();















    }

    function narrativeFirst () {

    //print ("debug 1");

    //var priorityCache = string1.Split(","[0]);

    var words = string1.Split(","[0]);
    print ("" + words );

    }
     
    Last edited: Jul 31, 2012
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not an error, it's printing exactly what you told it to print. "words" is in fact System.String[]. If you want the first item in the array, you have to specify it. (In the future, please use the code tags to format code.)

    --Eric