本文介绍了如何将文本转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个文本框,它们有一个String输入。
我需要将所有文本框中的所有文本转换为int格式。
我创建了一个函数convertToInt()但是我不确定该函数是如何创建的。
我有以下代码:
I have several text boxes which will have a String input.
I need to convert all of the text from all of the text boxes to int format.
I have created a function convertToInt() however I am unsure how the function would be created.
I have the following code:
week.addDay(1, (convertToInt(txtHrsMon.getText())), (convertToInt(txtMinsMon.getText())));
week.addDay(2, (convertToInt(txtHrsTues.getText())), (convertToInt(txtMinsTues.getText())));
week.addDay(3, (convertToInt(txtHrsWeds.getText())), (convertToInt(txtMinsWeds.getText())));
week.addDay(4, (convertToInt(txtHrsThurs.getText())), (convertToInt(txtMinsThurs.getText())));
week.addDay(5, (convertToInt(txtHrsFri.getText())), (convertToInt(txtMinsFri.getText())));
推荐答案
static void Main(string[] args)
{
string ipValue = Console.ReadLine();
int nmbr = 0;
if (!string.IsNullOrEmpty(ipValue))
nmbr = ConvertToInteger(ipValue);
Console.WriteLine(nmbr);
Console.ReadLine();
}
private static int ConvertToInteger(string p_ipValue)
{
int l_value = 0;
string l_valueStr = string.Empty;
try
{
foreach (var item in p_ipValue)
{
try
{
if (p_ipValue[0] == item && (p_ipValue.StartsWith("-") || p_ipValue.StartsWith("+")))
l_valueStr = item.ToString();
else
l_valueStr += int.Parse(item.ToString()).ToString();
}
catch
{
if (p_ipValue.Length > 1 && p_ipValue[1] == item)
{
if (l_valueStr.StartsWith("-") || l_valueStr.StartsWith("+"))
l_valueStr = "0";
}
else if (p_ipValue[0] == item)
l_valueStr = "0";
break;
}
}
}
catch
{
return 0;
}
return l_value = int.Parse(l_valueStr);
}
这篇关于如何将文本转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!