How can I convert String to Int?

后端 未结 30 2166
情歌与酒
情歌与酒 2020-11-21 05:35

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database.

How can I do this?

30条回答
  •  名媛妹妹
    2020-11-21 06:29

    Be careful when using Convert.ToInt32() on a char! It will return the UTF-16 code of the character!

    If you access the string only in a certain position using the [i] indexing operator, it will return a char and not a string!

    String input = "123678";
                        ^
                        |
    int indexOfSeven =  4;
    
    int x = Convert.ToInt32(input[indexOfSeven]);             // Returns 55
    
    int x = Convert.ToInt32(input[indexOfSeven].toString());  // Returns 7
    

提交回复
热议问题