Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

string functions

Discussion in 'Scripting' started by MrDude, Oct 25, 2007.

  1. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Hia

    Just spent a couple of hours researching all over the forums/ documentation/ web for string handling functions and just cannot find it.

    After a couple hours searching I found that the reason indexOf didn't work was because it is supposed to be IndexOf. Great! one down. More to go...

    I am trying to create a dialogue parser and need to split lines of text into separate components. Each line of text may have either TAB or SPACE separated elements and in many cases, both (and in some, none). I am looking for an effective way of splitting up lines like the following:

    Code (csharp):
    1.  
    2. [require][TAB]- readFile 1 79[TAB]- beenHere 1 85
    3. KEYS[TAB]- GoldCoin 200[TAB]+ newMission 1
    4.  
    ...where [TAB] is the actual tab ( \t ).

    In my old Pascal days I had to copy all text from beginning to first occurance of 'delimter' into one string then copy the rest from delimiter + 1 to end to the original string and repeat. Per-character parsing and multiple re-reads of the same text... very bad coding...

    Then I found in JavaScript the string.indexOf() and string.split() methods and that cleared things right up and made life so easy. When I realized that Unity runs on JavaScript I thought to myself this would be easy to code...

    I was wrong...

    Any assistance on how to effectively split text up into separate text and numeric variables would be most appreciated.

    With thanks :)

    MrDude

    p.s. Here is an actual extract from my dialogue file so you can see what I am trying to do.

    Code (csharp):
    1.  
    2. [line]  38
    3. [who]   1
    4. [next]  12
    5. That is amazing!
    6. Truly amazing...
    7.  
    8. [line]  39
    9. [who]   1
    10. [next]  33
    11. [require]   - reKySw 2 40
    12. [keys]  + reKySw 1
    13. This sounds easy enough, but could you please show me those keys again?
    14.  
    15. [line]  40
    16. [who]   0
    17. [next]  41
    18. You seem to be stuck on this.
    19. Would you like for me to skip the rest of this demo and go directly to
    20. the tutorial on how to define the keys?
    21.  
    22. [choice]    41
    23. [who]   1
    24. 69  Yes
    25. 42  No
    26.  
    [choice] and [line] identify wether the following is normal text or selectable options. This is followed by the line's ID.
    [who] points to an array index that contains the actor's name and avatar
    [next] points to the next line to display
    [require] lists a set of conditions that have to be met (defined below)
    [keys] define any alteration to game keys (custom class)
    Normal text is entered normally but options are preceded by the line of text they redirect to.

    Keys work like this:
    [kind of key] [keyName] [QTY] [redirect]
    So:
    + GoldCoins 200 5[TAB]- KnightGuild 1 20
    means that player must have 200+ gold coins or the thief will speak dialogue line 5. If the Player DOES have 200 gold GoldCoins but is a member of the knight's guild, the thief will have an alternate dialogue...

    So...erm... help... please?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think the main difficulty you're having is that Unity stuff is based on .NET, so there's no point in having Javascript string functions since they would be redundant. So you want to reference MSDN (rather poor) or Mono docs rather than Javascript docs.

    To split a string, do something like:

    Code (csharp):
    1. var someString = "This is a\t string with\t tabs\t in it";
    2. var stringArray = someString.Split("\t"[0]);
    Then stringArray will contain an array of strings split on tabs. (Theoretically...that's untested code....)

    --Eric
     
  3. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Thanks for that

    This is c# syntax, isn't it?

    I am not familiar with this syntax...
    "Split the string on the first character of the character I choose". If it works, then it works so I will try it , thanks... the syntax just eludes me...

    I tried it in JavaScript earlier on and found that string.split() is reported as not being a member of string class. On the other hand, string.Split() reports an overload error when I submit a string, a string and a number and also two strings. Sees it does have a split method, i just don't know the parameters...

    Now here is the question. I declare a custom class for this and have my functions inside the class. I assume I cannot have c# and javascript functions inside the same class. It will be interesting to see how this plays out...

    Thanks for the input.

    EDIT:
    Hold on... Does the [0] cast a string as a char?
    If that is the case, does this mean splits can be done only by character?
    Makes sense if this is so...
     
  4. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Yes, that particular Split function takes a char as a parameter (since you can't declare a char in Unity Javascript, "\t"[0] is a clever alternative). Here is a list of all the class members and functions for .NET's String class. Remember to stick to .NET version 2.0 or below.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nope. :) .Split is .NET, which all 3 languages in Unity use. (Which makes sense, since it's a lot less work that way.)

    You can, actually.

    Code (csharp):
    1. var splitChar : char = "#$%|"[3];
    2. var someString = "This|will|be|split";
    3. var splitStrings = someString.Split(splitChar);
    4.  
    There are a bunch of valid types you can use that Unitron doesn't highlight by default, for some reason. (Like "byte", among others.)

    --Eric