如何在ASP.NET C#中循环分割字符串 [英] How do I divide the string in loops in ASP.NET C#

查看:386
本文介绍了如何在ASP.NET C#中循环分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string rowValue = row [TrainNo]。ToString();





我在行值中存储train no的值并且想要将rowvalue划分为循环以找到确切的没有plz帮助as /



我尝试了什么:



 string rowValue = row [TrainNo]。ToString(); 

解决方案

如果你想把它变成一个双倍然后除以10000,你可以这样做:



  if (!double.TryParse(rowValue, out   double   value )){
// 无效格式。
}
/ = 10000 ;


虽然Thaddeus Jones的解决方案可行,但请帮个忙:不要将数据存储在VARCHAR或NVARCHAR列中,除非它是真的一个字符串。以适当的数据类型存储数据:INT列中的整数,FLOAT列中的浮点数,DATE,DATETIME或DATETIME2中的日期。它可以在以后节省很多问题,并且当您想要处理数据时,使代码变得更加容易,因为该列不能包含无效数据。如果你有一个整数的TrainNumber列,那么没有任何东西可以放入Jaipur或12K,导致你的代码失败,因为它不是一个数字。

然后你所要做的就是

 int rowValue =(int)row [TrainNo]; 

你走了......


< blockquote>根据您的说明,您的代码已被修改。我希望这适合你。



  string  rowValue =   12345,asdf,65431,10000; 
int 结果;

string [] splitString = rowValue.Split(' );
foreach string str in splitString)
{
if Int32 .TryParse(str, out 结果))
{

if (结果% 10000 == 0
{
Console.WriteLine(结果+ :需要);
}
else
{
Console.WriteLine(结果+ :不需要);
}

}
else
{
Console.WriteLine(str + :不需要);
}
}


string rowValue = row["TrainNo"].ToString();


im storing the value of train no in rowvalue and want to divide the rowvalue into the loops to find exact no plz help asap

What I have tried:

string rowValue = row["TrainNo"].ToString();

解决方案

If you want to turn it into a double and then divide it by 10000, you could do:

if (!double.TryParse(rowValue, out double value)) {
  // Invalid format.
}
value /= 10000;


While the solution by Thaddeus Jones will work, do yourself a favour: Don;t store data in VARCHAR or NVARCHAR columns unless it is genuinely a string. Store data in appropriate datatypes: integers in INT columns, floating point numbers in FLOAT columns, date in DATE, DATETIME, or DATETIME2. It saves a lot of problems later and makes your code much, much, easier when you want to work with the data, because the column can't contain invalid data. If you have an integer TrainNumber column, then nothing can put "Jaipur" or "12K" in there, causing your code to fail because it isn't a number.
Then all you have to do is

int rowValue = (int) row["TrainNo"];

And off you go...


Based on your clarifications, your code has been modified. I hope this works for you.

string rowValue = "12345,asdf,65431,10000";
           int result;

           string[] splitString = rowValue.Split(',');
           foreach (string str in splitString)
           {
               if(Int32.TryParse(str, out result))
               {

                   if (result % 10000 == 0)
                   {
                       Console.WriteLine(result +" : is needed");
                   }
                   else
                   {
                       Console.WriteLine(result +" : is not needed");
                   }

               }
               else
                   {
                       Console.WriteLine(str+" : is not needed");
                   }
           }


这篇关于如何在ASP.NET C#中循环分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆