Convertir un String en Int – Unity C#

Voici un petit script C# pour Unity, qui vous montre comment convertir une chaîne de caractères (String) en int.

using UnityEngine;

public class StringHelper : MonoBehaviour
{
    private void Start()
    {
        string testString = "456";

        print("Avant = " + (testString + 100));
        //Affiche : Avant = 456100
        //Le script effectue une concaténation

        int testInt = ConvertStringToInt(testString);

        print("Après = " + (testInt + 100));
        //Affiche : Après = 556
        //Le script effectue une addition
    }

    int ConvertStringToInt(string input)
    {
        int result;
       

        if (int.TryParse(input, out result))
        {
            return result;

        }

        return 0;
      
    }


}

Recevez les dernières actus

Nous ne spammons pas ! Consultez notre politique de confidentialité pour plus d’informations.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut