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

String.Split error

Discussion in 'Scripting' started by FisherM, Sep 15, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Here is the code
    Code (CSharp):
    1. string InventoryRawString = "x=1,y=1,ItemName=M4A1,quantity=1/x=3,y=1,ItemName=AK-47,quantity=1";
    2.         string[] InventoryItemStrings = InventoryRawString.Split ("/");
    3.         foreach (string ItemString in InventoryItemStrings){
    4.             string XString = ItemString.Split(","[0]);
    5.             int XItem = int.Parse(XString.Split("="[1]));
    6.             string YString = ItemString.Split(","[1]);
    7.             int YItem = int.Parse(YString.Split("="[1]));
    8.  
    9.             string NameString = ItemString.Split(","[2]);
    10.             string NameItem = NameString.Split("="[1]);
    11.             string QuanString = ItemString.Split(","[3]);
    12.             int QuanItem = int.Parse(QuanString.Split("="[1]));
    13.             addItem(XItem, YItem, NameItem, QuanItem);
    14.         }
    Popping 12 errors at the moment
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Instead of ItemString.Split(","[0]), I suspect you meant ItemString.Split(",")[0].

    But also, you're splitting the same string multiple times, which is wasteful and (as you found) error-prone... consider splitting each string just once, into a local variable, and then picking your parts out of that.
     
  3. AndyLL

    AndyLL

    Joined:
    Aug 25, 2013
    Posts:
    75
    What Joe said.

    However... what you are doing is painful. I assume the raw string is for some type of storage system.

    This is a perfect place to use JSON to parse strings into classes and vise versa. In the past I have also serialized classes to XML and back for storage.
     
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I split multiple times
     
  5. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    And thus I cannot split multiple times on the same object
     
  6. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    JSON seems inappropriate for my context
     
  7. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Still really need assistance with this
     
  8. AndyLL

    AndyLL

    Joined:
    Aug 25, 2013
    Posts:
    75
    Joe told you what your error was.

    Instead of ItemString.Split(","[0]), I suspect you meant ItemString.Split(",")[0].
     
  9. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    That doesn't work and the documentation in unit for string split says that isn't how it is written?
     
  10. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Code currently looks like this
    Code (CSharp):
    1. string InventoryRawString = "x=1,y=1,ItemName=M4A1,quantity=1/x=3,y=1,ItemName=AK-47,quantity=1";
    2.         string[] InventoryItemStrings = InventoryRawString.Split ("/");
    3.         foreach (string ItemString in InventoryItemStrings){
    4.             string XString = ItemString.Split(",")[0];
    5.             int XItem = int.Parse(XString.Split("=")[1]);
    6.             string YString = ItemString.Split(",")[1];
    7.             int YItem = int.Parse(YString.Split("=")[1]);
    8.  
    9.             string NameString = ItemString.Split(",")[2];
    10.             string NameItem = NameString.Split("=")[1];
    11.             string QuanString = ItemString.Split(",")[3];
    12.             int QuanItem = int.Parse(QuanString.Split("=")[1]);
    13.             addItem(XItem, YItem, NameItem, QuanItem);
    24 errors I am getting
    it continues
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yeah, sorry, I only pointed out the most obvious error. This one is more subtle: String.Split takes an array of char or string, not a single string. (One of the dumber choices in the whole .NET framework, IMHO.) Fortunately there's one overload that takes a param array of char (that is, you can specify any number of character parameters).

    So, if you just change your double quotes to single quotes, it should work.
     
    exafred likes this.
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Why?

    I'll also point out to others posting that ","[0] is a char. Indexer of a string returns the character at that position. If you need a char[] then you'd have to write new char[1] { ',' }
     
    Last edited: Sep 15, 2014
  13. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Code (CSharp):
    1. string InventoryRawString = "x=1,y=1,ItemName=M4A1,quantity=1/x=3,y=1,ItemName=AK-47,quantity=1";
    2. string[] InventoryItemStrings = InventoryRawString.Split('/');
    3. foreach (string ItemString in InventoryItemStrings) {
    4.     string XString = ItemString.Split(',')[0];
    5.     int XItem = int.Parse(XString.Split('=')[1]);
    6.     string YString = ItemString.Split(',')[1];
    7.     int YItem = int.Parse(YString.Split('=')[1]);
    8.  
    9.     string NameString = ItemString.Split(',')[2];
    10.     string NameItem = NameString.Split('=')[1];
    11.     string QuanString = ItemString.Split(',')[3];
    12.     int QuanItem = int.Parse(QuanString.Split('=')[1]);
    13.     addItem(XItem, YItem, NameItem, QuanItem);
    14. }
    15.  
    Here's your code fixed FisherM! Hope this helps. :)
    The problem was that you were using double quote enclosed strings as the Split parameter, Split takes a char value. so changing the double quotes to single quotes you change all of them to characters which is what the Split function takes, no idea why no one else saw that! =D

    ~
    Another walk in the park.
     
    exafred and image28 like this.
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, no one except KelsoMRK in the previous post, and me in the post before that. :)
     
    BmxGrilled likes this.
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There's also something to be said for suggesting a different solution that might work out better in the long run instead of just fixing the problem in front of your face. :)