Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved] String.Split not working?

Discussion in 'Scripting' started by medsouz, Jul 8, 2010.

  1. medsouz

    medsouz

    Joined:
    Jun 2, 2010
    Posts:
    259
    Here is my code:
    Code (csharp):
    1. var DataStr=w.data;
    2. Data = DataStr.Split(":");
    3. Ter = Data[0];
    4. Owner = Data[1];
    It keeps giving me this error:
    Help please?

    EDIT: I figured it out String.Split has to be lile this:
    Code (csharp):
    1. Data = DataStr.Split(":"[0]);
     
    Kasia_Wieciorek likes this.
  2. Creative

    Creative

    Joined:
    May 8, 2010
    Posts:
    184
    (":",[0])
     
    Kasia_Wieciorek likes this.
  3. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    Code (csharp):
    1.  
    2.     var str : String = "tom.mom.mot";
    3.     var arr = new Array(); 
    4.     arr = str.Split(".", [0]);
    5.  
    6. // arr[0] = tom
    7. // arr[1] = mom
     
    Kasia_Wieciorek likes this.
  4. Creative

    Creative

    Joined:
    May 8, 2010
    Posts:
    184
    You're too late!@Tempest :D
     
  5. medsouz

    medsouz

    Joined:
    Jun 2, 2010
    Posts:
    259
    Wow I figured this out right as you guys posted...
     
  6. Creative

    Creative

    Joined:
    May 8, 2010
    Posts:
    184
    great :)
     
  7. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    It's probably clearer syntax if you type it like this:

    Data = DataStr.Split(':');


    Split is looking for a char, so use the char quotes.
     
  8. Yuta.M

    Yuta.M

    Joined:
    Mar 30, 2011
    Posts:
    13
    Creative's and Tempest's sample code is wrong.
    You should write "."[0].
    You must not write comma.
     
    Last edited: Jun 9, 2011
  9. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Why?what's [0]......
     
    Kasia_Wieciorek likes this.
  10. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    it should be

    Code (csharp):
    1.  
    2.  string[] Data = DataStr.Split(':');
    3.  
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    The original code (from years ago!) is in Unityscript, so no that won't work. ":"[0] is correct.

    --Eric
     
  12. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    My bad, i thought that was c#.

    Will pay more attention :D