字符串和整数比较 [英] String and int comparison
本文介绍了字符串和整数比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
foreach (String entry in GetPageParams().AllKeys)
{
id = Convert.ToInt32(GetPageParams<string>("hdnHistorySave"));
if (entry.StartsWith("txtMessage"))
{
TicketId = entry.ToString().Substring(10);
if (id == Convert.ToInt32(TicketId))
{
objTicket.TicketDetailId = id;
objTicket.Message = GetPageParams<string>("txtMessage"+id);
}
}
}
有可能这样做吗?因为在id == TicketId正在检查..时出现错误(输入字符串格式不正确"),请帮忙!
Is it possible to do like this?? Because am getting an error ("input string not i correct format") at the id==TicketId Checking .. Please help !
推荐答案
您可以比较两个值相同类型.
如果您有字符串"12"和数字2,则可以将字符串转换为数字,然后将其与2进行比较.
如果您的字符串是"A12",那么您将无法将其转换为数字.
为避免在将字母数字字符串转换为数字时出错,可以使用TryParse
方法 [^ ].这将帮助您避免转换时出错.
You can compare two values of the same type.
If you have a string "12" and a number 2, you can convert the string to a number and then compare it with 2.
If your string is "A12", then you won''t be able to convert this to a number.
To avoid an error while converting an alphanumeric string to a number, you can use the TryParse
method[^]. This will help you avoid an error while conversion.
这篇关于字符串和整数比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文