Simon Surename
Simon Surename

Reputation: 23

Converting strings to int in C#

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. (:

Upvotes: 1

Views: 11958

Answers (5)

Andrey Gordeev
Andrey Gordeev

Reputation: 32509

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

Upvotes: 5

Leep
Leep

Reputation: 457

Try this,

 string SubString = MyString.Substring(1);

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

Upvotes: 1

adt
adt

Reputation: 4360

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

Upvotes: 0

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29223

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);

Upvotes: 1

Wesley Lomax
Wesley Lomax

Reputation: 2077

You can use Regex to replace all alpha characters eg

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

Upvotes: 0

Related Questions