我有一个我不明白的问题。这是我的代码:
String input = "3 days ago"
String firstCharacter = input[0].ToString(); //Returns 3
int firstCharacter = (int)input[0]; //Returns 51
为什么它会返回51?
PS:我的代码来自这个帖子:C#: how to get first char of a string?
更多信息:
In case that input = "5 days ago", then int firstCharacter is 53.
答案 0 :(得分:5)
以这种方式将char
投射到int
将为您提供 ASCII 值,其中3等于51.您可以在此处找到完整列表:
你想做这样的事情:
Char.GetNumericValue(input[0]);
答案 1 :(得分:1)
您还可以使用 substring 将其提取为字符串而不是字符串,并避免必须将其强制转换:
var getValues = function getValues(fields, state) {
return fields.reduce(function (accumulator, field) {
getValue(field, state, accumulator);
return accumulator;
}, {});
};