本文介绍了我有一个字符串数组,我想在类型为datetime时执行一些操作,如果不是我想要执行的其他操作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组。它有两种类型的值Double和DateTime。我想在第一个索引时执行一些操作Datetime还有其他操作。



string receivedData =77.6867#25.437#2018.01.08 14:58:28#12.8241#77.6867#25.375#2018.01.08 14:58:28#12.8241#77.6867#25.437#2018.01.08 14:58:346 #25.437#2018.01.08 15:03:20#12.8241#77.6866#25.437#2018.01.08 1#25.437#2018.01.08 14:24:06#12.8241#77.6866#25.312#2018.01.08 14:24:06#12.8241 #77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375# 2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08

删除delemeter后。我总是希望数组的第二个元素是Datetime。



我尝试了什么:



Thread.Sleep(10000);

SerialPort sp =(SerialPort)发件人;

serialPortData = sp.ReadExisting();

int indx = serialPortData.IndexOf(#);

serialPortData = serialPortData.Remove(0,indx + 1);

string [] Data = serialPortData.Split(new string [] {#},

StringSplitOptions。 RemoveEmptyEntries);

while(Data [1] .GetType()!= typeof(System.DateTime))

{

Data = Data .Skip(1)。ToArray();

}

这段代码没有显示任何错误。

当我把==在循环中,它循环退出所有值的循环。

当我把!=放在循环中时它继续执行相同的循环直到最后。

谢谢你提前。

I have a array of string .which have two types of values Double and DateTime .And i want to perform some operation when at the first index Datetime comes else other operation.

string receivedData="77.6867#25.437#2018.01.08 14:58:28#12.8241#77.6867#25.375#2018.01.08 14:58:28#12.8241#77.6867#25.437#2018.01.08 14:58:346#25.437#2018.01.08 15:03:20#12.8241#77.6866#25.437#2018.01.08 1#25.437#2018.01.08 14:24:06#12.8241#77.6866#25.312#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08"
After Removing the delemeter .I always want the 2nd element of the array is of Datetime.

What I have tried:

Thread.Sleep(10000);
SerialPort sp = (SerialPort)sender;
serialPortData = sp.ReadExisting();
int indx = serialPortData.IndexOf("#");
serialPortData = serialPortData.Remove(0, indx + 1);
string[] Data = serialPortData.Split(new string[] { "#" },
StringSplitOptions.RemoveEmptyEntries);
while(Data[1].GetType()!=typeof(System.DateTime))
{
Data = Data.Skip(1).ToArray();
}
this code is not showing any error.
when i put "==" inside while loop it exiting the loop for all values.
when i Put "!=" inside while loop it keep on executing the same loop till last.
thank u in advance.

推荐答案

DateTime dt;
if (DateTime.TryParseExact(Data[1], "yyyy.MM.dd hh:mm:ss", 
    CultureInfo.InvariantCulture, DateTimeStyles.None, dt)) 
{
    // Is a DateTime
}

double dVal;
// en-US for the period as decimal point
CultureInfo enUS = new CultureInfo("en-US"); 
// You might need to add styles or change it (e.g. for leading sign)
if (Double.TryParse(Data[1], NumberStyles.AllowDecimalPoint, enUS, dVal)) 
{
    // Is a double
}


string[] Data = serialPortData.Split(new string[] { "#" },



你有字符串数据 - 所以它永远不会有类型datetime。

我建议你仔细查看每个条目并尝试将其解析为DateTime





foreach(数据中的字符串strData) $

....

DateTime dtData = DateTime.Parse(strData); / /或者如果不是shure则使用tryparse

}


you have string data - so it will never have the type datetime.
I'd suggest just go through each entry and try to parse it into a DateTime


foreach(string strData in Data)
{
.... for every second element or each element that could be converted
DateTime dtData = DateTime.Parse(strData); // Or use tryparse if not shure
}



这篇关于我有一个字符串数组,我想在类型为datetime时执行一些操作,如果不是我想要执行的其他操作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:40