wpf C#中将字符串转换为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16790337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:52:50  来源:igfitidea点击:

Converting strings to int in C#

c#wpfstringint

提问by Simon Surename

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, d13where the character represents the colourand 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.

我对 C# 很陌生,我想做一个纸牌游戏。我所拥有的是一张卡片(字符串)列表,其名称类似于c6, s3, h11, d13wherecharacter represents the colournumber represents the value. 当按下按钮时,程序从列表中随机抽取一个字符串并将其显示在文本框中。从那里开始,游戏的重点是猜测下一张随机卡的值是比前一张卡更高还是更低。

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 cin c6so i can convert it by using parse.

我想要做的是从文本框中取出字符串并将其转换为 int,以便我可以将前一张卡片的值与新卡片的值进行比较。唯一的问题是我如何摆脱cinc6以便我可以使用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. (:

预先感谢您阅读本文。(:

采纳答案by Leep

Try this,

尝试这个,

 string SubString = MyString.Substring(1);

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

但要注意字符串是否为空,这是一个错误案例。

回答by Andrey Gordeev

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

我最好创建一个类Card,它代表了一张名片,2个属性:ColorNumber实施方法Card.ParseFromString()

回答by Theodoros Chatzigiannakis

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

int number = Int32.Parse(numberAsString);

回答by adt

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

回答by Wesley Lomax

You can use Regex to replace all alpha characters eg

您可以使用 Regex 替换所有字母字符,例如

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