[英]How to get a substring from a list of items and assign substring value to the same item in C#
StartTime![]() |
AgentId![]() |
---|---|
08/19/2021 07:04:56 AM UTC ![]() |
33 ![]() |
08/11/2021 02:58:35 PM IST ![]() |
42 ![]() |
08/12/2021 01:01:51 AM CST ![]() |
24 ![]() |
08/12/2021 08:52:34 PM UTC ![]() |
61 ![]() |
public class MyModel
{
public string StartTime { get; set; }
public int AgentId { get; set; }
public string TimeZone { get; set; }
public string Time { get; set; }
}
I have the above C#
Class model MyModel
and its sample data stored as a List<MyModel>
in my logic.我有上面的
C#
Class model MyModel
及其样本数据在我的逻辑中存储为List<MyModel>
。 I need a help to parse the StartTime
field of a list such that, the TimeZone
field should get the UTC/CST/IST
of its corresponding StartTime
and Time
field of a list should be time stamp from its StartTime
i,e: 07:04:56
or 02:58:35
and so on.我需要帮助来解析列表的
StartTime
字段,这样, TimeZone
字段应该获取其相应StartTime
的UTC/CST/IST
,列表的Time
字段应该是其StartTime
的时间戳,即:07: 07:04:56
或02:58:35
等等。 Finally the list should look like something like below:最后,列表应如下所示:
StartTime![]() |
AgentId![]() |
Time![]() |
TimeZone![]() |
---|---|---|---|
08/19/2021 07:04:56 AM UTC ![]() |
33 ![]() |
07:04:56 AM![]() |
UTC![]() |
08/11/2021 02:58:35 PM IST ![]() |
42 ![]() |
02:58:35 PM![]() |
IST ![]() |
08/12/2021 01:01:51 AM CST ![]() |
24 ![]() |
01:01:51 AM![]() |
CST![]() |
08/12/2021 08:52:34 PM UTC ![]() |
61 ![]() |
08:52:34 PM![]() |
UTC![]() |
Given the fact that all of the segments in your StartTime
property have a forced amount of characters thanks to the string format (except for the timezone where we could have eg CEST but that doesn't matter since it's the last segment and I'll get to the why later) we can simply use the String.Substring
method and for the first parameter, define the character from where the newly to be output string should start reading and as the second parameter, the amount of characters it should read from there on out.鉴于您的
StartTime
属性中的所有段都具有强制数量的字符,这要归功于字符串格式(除了我们可以拥有例如 CEST 的时区,但这并不重要,因为它是最后一个段,我会得到到后面为什么)我们可以简单地使用String.Substring
方法,对于第一个参数,定义新成为 output 字符串应该开始读取的字符,作为第二个参数,它应该从那里读取的字符数量出去。
With that in mind I adjusted your class to this:考虑到这一点,我将您的 class 调整为:
public class MyModel
{
public string StartTime { get; set; }
public int AgentId { get; set; }
public string TimeZone => this.StartTime.Substring(23);
public string Time => this.StartTime.Substring(11, 11);
}
For the TimeZone
property you can see that we only passed one parameter.对于
TimeZone
属性,您可以看到我们只传递了一个参数。 The default behaviour for not passing the second parameter simply means "start reading at index of the parameter and read the string to its end" which is also why a different amount of possible characters in the timezone is nothing we have to worry about.不传递第二个参数的默认行为只是意味着“从参数的索引处开始读取并将字符串读取到其末尾”,这也是为什么我们不必担心时区中可能出现的不同数量的字符的原因。
The way this solution works is, whenever you access either the TimeZone
or Time
property, the code will be executed returning the substring that you need.此解决方案的工作方式是,每当您访问
TimeZone
或Time
属性时,将执行代码并返回您需要的 substring。 Performance wise there would be a disadvantage where the code would be executed every time you call either of the properties even when the StartTime
value hasn't changed.性能方面会有一个缺点,即每次调用任一属性时都会执行代码,即使
StartTime
值没有更改也是如此。
Alternatively you could do it the other way around where every time you pass a new value to the StartTime both of the properties would be updated as well:或者,您可以以另一种方式执行此操作,每次将新值传递给 StartTime 时,两个属性也会更新:
public class MyModel
{
private string _startTime;
public string StartTime
{
get => _startTime;
set
{
this._startTime = value;
this.TimeZone = value.Substring(23);
this.Time = value.Substring(11, 11);
}
}
public int AgentId { get; set; }
public string TimeZone { get; private set; }
public string Time { get; private set; }
}
This eliminates previously mentioned disadvantage but creates a new one where the TimeZone
and Time
property will always be updated whenever the StartTime property is, even if you wouldn't use it.这消除了前面提到的缺点,但创建了一个新的缺点,其中
TimeZone
和Time
属性将始终在 StartTime 属性出现时更新,即使您不使用它也是如此。
Personally, I'd stick to the first option simply because it's shorter and easier to read.就个人而言,我会坚持第一个选项,因为它更短且更易于阅读。
You can use this sample and run code in this link :您可以使用此示例并在此链接中运行代码:
class Program
{
static void Main(string[] args)
{
var list = new List<string>
{
"08/19/2021 07:04:56 AM UTC ",
"08/11/2021 02:58:35 PM IST ",
"08/12/2021 01:01:51 AM CST ",
"08/12/2021 08:52:34 PM UTC "
};
var list2 = new List<DateInfo>();
list.ForEach(e =>
{
e = e.Trim();
var dt = DateTime.Parse(e.Substring(0, e.Length - 3)).Date;
var tm = DateTime.Parse(e.Substring(0, e.Length - 3)).TimeOfDay;
list2.Add(new DateInfo
{
StartTime = dt,
Time = tm,
TimeZone = e[^3..]
});
});
list2.ForEach(e =>
{
Console.WriteLine(e.ToString());
});
}
}
public class DateInfo
{
public DateTime StartTime { get; set; }
public TimeSpan Time { get; set; }
public string TimeZone { get; set; }
public override string ToString()
{
return ($"StartTime : {StartTime:yyyy/MM/dd}, Time : {Time}, TimeZone : {TimeZone}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.