Hap*_*ird -3 c# string casting
我有一个我不明白的问题.这是我的代码:
String input = "3 days ago"
String firstCharacter = input[0].ToString(); //Returns 3
int firstCharacter = (int)input[0]; //Returns 51
Run Code Online (Sandbox Code Playgroud)
为什么它会回归51?
PS:我的代码来自这个帖子:C#:如何获取字符串的第一个字符串?
更多信息:
In case that input = "5 days ago", then int firstCharacter is 53.
Run Code Online (Sandbox Code Playgroud)
char以int这种方式转换为a 将为您提供其ASCII值,其中3等于51.您可以在此处找到完整列表:
你想做这样的事情:
Char.GetNumericValue(input[0]);
Run Code Online (Sandbox Code Playgroud)