String Split

Hello, I have this script:

using UnityEngine;
using System.Collections;

public class StringSplit : MonoBehaviour {

    public int test = 1234;
    public string abc;
    public string ausgabe;
    public string asdf;
    public int i = 1;

    public int zahl1;
    public int zahl2;
    public int zahl3;
    public int zahl4;
    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        abc = test.ToString();
       
       
       
        foreach (char c in abc)
        {
            
            if (test < 10)
            {
                zahl1 = c;
                i = 0;
            }
            if (test < 100 && test > 9)
            {
                if (i == 1)
                {
                    zahl1 = c;
                }
                if (i == 2)
                {
                    zahl2 = c;
                    i = 0;
                }
            }

            if (test < 1000 && test > 99)
            {
                if (i == 1)
                {
                    zahl1 = c;
                }

                if (i == 2)
                {
                    zahl2 = c;
                }
                if (i == 3)
                {
                    zahl3 = c;
                    i = 0;
                }
            }
            if (test < 10000 && test > 999)
            {
                if (i == 1)
                {
                    zahl1 = c;
                }
                if (i == 2)
                {
                    zahl2 = c;
                }
                if (i == 3)
                {
                    zahl3 = c;
                }
                if (i == 4)
                {
                    zahl4 = c;
                    i = 0;
                }
            }
            i++;
           
        }

    }

   
}

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?

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(…)

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.

[LIST=1]
[*]if (test < 10000 && test > 999)
[*]           {
[*]               if (i == 0)
[*]               {
[*]                    zahl1 = c;
[*]               }
[*]               if (i == 1)
[*]               {
[*]                    zahl2 = c;
[*]               }
[*]               if (i == 2)
[*]               {
[*]                    zahl3 = c;
[*]               }
[*]               if (i == 3)
[*]               {
[*]                    zahl4 = c;
[*]                    i = 0;
[*]               }
[*]           }
[*]            i++;
[/LIST]

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:

            int test = 5975;
            int zahl1 = 0;
            int zahl2 = 0;
            int zahl3 = 0;
            int zahl4 = 0;

            zahl1 = test / 1000;
            zahl2 = (test % 1000) / 100;
            zahl3 = ((test % 1000) % 100)  / 10;
            zahl4 = ((test % 1000) % 100) %10;

Hope it does what you want.

Yes, it works! Thanks! :slight_smile: