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

Discussion in 'Scripting' started by TechnoObi, Sep 3, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Hello, I have this script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class StringSplit : MonoBehaviour {
    6.  
    7.     public int test = 1234;
    8.     public string abc;
    9.     public string ausgabe;
    10.     public string asdf;
    11.     public int i = 1;
    12.  
    13.     public int zahl1;
    14.     public int zahl2;
    15.     public int zahl3;
    16.     public int zahl4;
    17.     // Use this for initialization
    18.     void Start () {
    19.        
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         abc = test.ToString();
    25.        
    26.        
    27.        
    28.         foreach (char c in abc)
    29.         {
    30.            
    31.             if (test < 10)
    32.             {
    33.                 zahl1 = c;
    34.                 i = 0;
    35.             }
    36.             if (test < 100 && test > 9)
    37.             {
    38.                 if (i == 1)
    39.                 {
    40.                     zahl1 = c;
    41.                 }
    42.                 if (i == 2)
    43.                 {
    44.                     zahl2 = c;
    45.                     i = 0;
    46.                 }
    47.             }
    48.  
    49.             if (test < 1000 && test > 99)
    50.             {
    51.                 if (i == 1)
    52.                 {
    53.                     zahl1 = c;
    54.                 }
    55.  
    56.                 if (i == 2)
    57.                 {
    58.                     zahl2 = c;
    59.                 }
    60.                 if (i == 3)
    61.                 {
    62.                     zahl3 = c;
    63.                     i = 0;
    64.                 }
    65.             }
    66.             if (test < 10000 && test > 999)
    67.             {
    68.                 if (i == 1)
    69.                 {
    70.                     zahl1 = c;
    71.                 }
    72.                 if (i == 2)
    73.                 {
    74.                     zahl2 = c;
    75.                 }
    76.                 if (i == 3)
    77.                 {
    78.                     zahl3 = c;
    79.                 }
    80.                 if (i == 4)
    81.                 {
    82.                     zahl4 = c;
    83.                     i = 0;
    84.                 }
    85.             }
    86.             i++;
    87.            
    88.         }
    89.  
    90.     }
    91.  
    92.    
    93. }
    94.  
    But when I wanna split "1234" I get:
    zahl1 = 50, zahl2 = 51, zahl3 = 52 and zahl4 = 49, but I want zahl1 = 1, zahl2 = 2, zahl3 = 3 and zahl4 = 4. Where is my error?
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    two problems...

    1. you should be testing against i = 0,1,2,3 (not 1,2,3,4)

    2. youre assigning a char to an int. (the int value for 1 is most likely 50 without looking it up). You need to convert from char to int.. off the top of my head I dont think a cast will give you the result you want. Use Convert.ToInt(..)
     
  3. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    ok, i have now :
    zahl1 = (int)char.GetNumericValue(c);
    this works now better, but now:
    zahl1 = 2, zahl2 = 3, zahl3 = 4 and zahl4 = 1. I don't know, what you mean with 1.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2.  
    3. [LIST=1]
    4. [*]if (test < 10000 && test > 999)
    5. [*]           {
    6. [*]               if (i == 0)
    7. [*]               {
    8. [*]                    zahl1 = c;
    9. [*]               }
    10. [*]               if (i == 1)
    11. [*]               {
    12. [*]                    zahl2 = c;
    13. [*]               }
    14. [*]               if (i == 2)
    15. [*]               {
    16. [*]                    zahl3 = c;
    17. [*]               }
    18. [*]               if (i == 3)
    19. [*]               {
    20. [*]                    zahl4 = c;
    21. [*]                    i = 0;
    22. [*]               }
    23. [*]           }
    24. [*]            i++;
    25. [/LIST]
    26.  
    27.  
     
  5. THoeppner

    THoeppner

    Joined:
    Oct 10, 2012
    Posts:
    205
    Wow, it took me some time to understand what you want to do. And I'm not sure if I really got it. If you want some real help try to explain what you want to reach.

    For my understanding you try to spilt your numer in the seperate digits. Is this correct? If so try this piece of good:

    Code (CSharp):
    1.            
    2.             int test = 5975;
    3.             int zahl1 = 0;
    4.             int zahl2 = 0;
    5.             int zahl3 = 0;
    6.             int zahl4 = 0;
    7.  
    8.             zahl1 = test / 1000;
    9.             zahl2 = (test % 1000) / 100;
    10.             zahl3 = ((test % 1000) % 100)  / 10;
    11.             zahl4 = ((test % 1000) % 100) %10;
    12.  
    Hope it does what you want.
     
  6. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Yes, it works! Thanks! :)