I have a string that looks like this
"Start Date:
2015-1-1
End Date:
2017-1-1
Warranty Type:
XXX
Status:
Active
Serial Number/IMEI:
XXXXXXXX
Description:
This product has a three year limited warranty and is entitled to parts, labor and on-site repair service. Service is available Monday-Friday, except holidays, with a next business day response objective. Many parts can also be delivered to you using the Customer Replaceable Unit (CRU) method."
I would like to store "2015-1-1" "2017-1-1" "Active" in my Data table- How is this done?
Once that process is done I want to compare the data I have stored to my original data table and see if the dates and status' match (if they dont I create a report alerting the user there is a date that is wrong).
Edit: I have around 300~ of these strings so I am not sure what the best way to handle that amount of strings is.
You may try something like this:
var parts = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var startDate = DateTime.Parse(parts.SkipWhile(p => p != "Start Date:").Skip(1).First().Trim());
var endDate = DateTime.Parse(parts.SkipWhile(p => p != "EndDate:").Skip(1).First().Trim());
var status = parts.SkipWhile(p => p != "Status:").Skip(1).First();
then, having these values, you may insert them into your data table
This is a perfect solution for Regular Expressions.
EDIT: Since OP updated his input string, I updated my code and regular expression pattern to match.
string var1 =
@"Start Date:
2015 - 1 - 1
End Date:
2017 - 1 - 1
Warranty Type:
XXX
Status:
Active
Serial Number / IMEI:
XXXXXXXX
Description:
This product has a three year limited warranty and is entitled to parts, labor and on-site repair service. Service is available Monday - Friday, except holidays, with a next business day response objective. Many parts can also be delivered to you using the Customer Replaceable Unit (CRU)method.";
// Remove all spaces and newlines
var1 = var1.Replace(" ", "").Replace("\r\n", "");
// our regex pattern now that there ar eno spaces or new lines
var regex = new Regex(@"StartDate:(.*)EndDate:(.*)W.*Status:(.*)\/");
//assign them
string strStartDate = regex.Match(var1).Groups[1].ToString();
string strEndDate = regex.Match(var1).Groups[2].ToString();
string Status = regex.Match(var1).Groups[3].ToString().Substring(0, 6);
//convert to DateTime from our string versions
DateTime StartDate = DateTime.Parse(strStartDate);
DateTime EndDate = DateTime.Parse(strEndDate);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.