Converting strings to int in C#

半腔热情 提交于 2020-04-30 07:49:36

问题


im pretty new to C# and i want to make a cardgame. What i have is a list of cards (strings) with names like c6, s3, h11, d13 where the character represents the colour and the number represents the value. When pressing a button the program takes a random string from the list and displays it in a textbox. From there the point of the game is to guess if the next random card will have a higher value or a lower value then the previous card.

What i want to do is to take the string from the textbox and turn it into a int so that i can compare the value of the previous card with the new one. Only problem is how do i get rid of the c in c6 so i can convert it by using parse.

This is my code.

public partial class MainWindow : Window
{
    static Random rndmlist = new Random();
    Random rndm = new Random();
    List<string> deck = new List<string>();
    int score = 0;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void test_Click(object sender, RoutedEventArgs e)
    {
        //disregard this
        foreach (string j in deck)
        {
            testbox.Text += j + ", ";
        }
    }

    private void btnstart_Click(object sender, RoutedEventArgs e)
    {
        //this is where i add all the cards to the list
        for (int i = 1; i <= 13;)
        {
            deck.Add("c" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("s" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("h" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("d" + i);
            i++;
        }
    }

    private void btnbegin_Click(object sender, RoutedEventArgs e)
    {
        //this is where i take a random card from the list and display it in textBox2
        int r = rndmlist.Next(deck.Count);
        textBox2.Text = ((string)deck[r]);

        //disregard this
        testbox.Text += ((string)deck[r]) + ", ";
        deck.Remove((string)deck[r]);
    }

    private void btnhigh_Click(object sender, RoutedEventArgs e)
    {
        //this is where i want to compare the cards.
    }
}

Thank you in advance for reading this. (:


回答1:


Try this,

 string SubString = MyString.Substring(1);

But take care if the string is empty, it is an error case.




回答2:


I'd better create a class Card, which represents a card, with 2 properties: Color and Number and implemented method Card.ParseFromString()




回答3:


Assuming there will always be one (and only one) character before the number, you can simply do this:

string numberAsString = "c2".Substring(1);

And to make that an int:

int number = Int32.Parse(numberAsString);



回答4:


You can use Regex to replace all alpha characters eg

string result = Regex.Replace(myString, @"[a-zA-Z\s]+", string.Empty);



回答5:


string str = "c12";
var noChars = str.SubString(1); // take a new string from index 1
var number = Int32.Parse(noChars);


来源:https://stackoverflow.com/questions/16790337/converting-strings-to-int-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!