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

Help With Text Parsing C#

Discussion in 'Scripting' started by bfata, Apr 24, 2014.

  1. bfata

    bfata

    Joined:
    Apr 19, 2013
    Posts:
    4
    Hi Everyone! I'm still pretty new to Unity3D, coding, and scripting in general, so please excuse what might be a n00b question. So, I'm trying to parse large amounts of information held in an Excel spreadsheet. 613 entries to be exact. I'm doing it like so:
    Code (csharp):
    1.  
    2. void Start ()
    3.         {
    4.                 // Make sure there this a text
    5.                 // file assigned before continuing
    6.                 if (textFile != null) {
    7.                         // Add each line of the text file to
    8.                         // the array using the new line
    9.                         // as the delimiter
    10.                         textData = (textFile.text.Split ('\n'));
    11.                 }
    12.                
    13.                 print ("the array is " + textData.Length + " characters long");
    14.         }
    15.  
    This works great, except the problem is that when I initially get the information there is a much of stuff before the numbers I want to get, i.e "2013-11-5;" So, for example the information in the cell will be something like "2013-11-5; 8000", where 8000 is the only thing I'm looking for.

    So my question, how would I parse through the text, only grabbing the number AFTER the " ; " symbol? Currently, I'm going through the spreadsheet, deleting the access information, but that is ridiculous and a total pain in the ass. Thank you in advance. Any help is appreciated.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    So, there are several .net libraries out their you can use to read and parse excel spreadsheets.

    I don't remember what the format of excel spreadsheets are, I do know that they're basically an exapansion on the csv format (to support macros and pages and other stuff).

    I also know that excel can save simple spreadsheets (with out macros and links and what not) as a csv file.

    Csv files are really easy to parse. Csv stands for 'comma separated values'. And really all it is is that each row is separated by a return line, and every column is separated by a ,. You can create a jagged array of these values like so:

    Code (csharp):
    1.  
    2. //validate your txtfile exists and what not before this code
    3.  
    4. var rows = txtFile.text.Split('\n');
    5. var data = new string[rows.Length] {};
    6. for(int i = 0; i < data.Length; i++)
    7. {
    8.     data[i] = rows[i].Split(',');
    9. }
    10.  
    In that, if you wanted to access the 3rd row, second column's value, you'd do something like:

    (array's use 0-based indices... 0,1,2... 2 is the 3rd entry)
    Code (csharp):
    1.  
    2. var val = data[2][1];
    3.  
     
  3. bfata

    bfata

    Joined:
    Apr 19, 2013
    Posts:
    4
    Thanks for the reply. I'm confused by the 4th line of the code however (I'm doing this in C#). Right now I have:

    Code (csharp):
    1.  
    2. if (textFile != null) {
    3.                         // Add each line of the text file to
    4.                         // the array using the new line
    5.                         // as the delimiter
    6.             textData = (textFile.text.Split ('\n'));
    7.             data = textData.Length;
    8.             for(int i = 0; i < data.Length; i++){
    9.             data[i] = textData[i].Split(',');
    10.             }
    11.  
    So, where you have "row" I have textData. How do I correctly assign the data string? Sorry... this is so n00b.
     
  4. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Please post javascript working code.