假设我在内部几乎与ISO类似的格式中有以下日期/时间:
"2011-11-07T11:17"
"--T11:17"
(上午11:17,没有约会,只有时间)"-11-07"
(11月7日,没有年份,没有时间)分隔符是必需的,使我能够知道数据是否存在。数据将设置为如下结构:
struct MyDate
{
int? Year ;
int? Month ;
int? Day ;
int? Hour ;
int? Minute ;
}
“最简单”的方法是逐个字符循环,并提取数据(如果存在)。
但我有一种唠叨的印象,即必须提取某种API,例如,一个整数并返回第一个非整数字符的索引(类似于C的strtol
)。
C#中是否有类似strtol
的函数,或者提取类型数据的高级函数,而不是按字符解析字符串?
答案 0 :(得分:1)
您可以使用DateTime.ParseExact
。查看文档here
答案 1 :(得分:1)
如果不考虑性能,我会使用正则表达式来完成任务。
使用(\d*)-(\d*)-(\d*)T(\d*):(\d*)
作为表达式,然后将每个捕获组解析为结构的相应字段,将空字符串转换为null
值:
var match = Regex.Match(str, @"(\d*)-(\d*)-(\d*)T(\d*):(\d*)");
var date = new MyDate();
if (match.Groups[1].Value != "") date.Year = int.Parse(match.Groups[1].Value);
if (match.Groups[2].Value != "") date.Month = int.Parse(match.Groups[2].Value);
if (match.Groups[3].Value != "") date.Day = int.Parse(match.Groups[3].Value);
if (match.Groups[4].Value != "") date.Hour = int.Parse(match.Groups[4].Value);
if (match.Groups[5].Value != "") date.Minute = int.Parse(match.Groups[5].Value);
编辑:此代码中必须使用'T'和':'分隔符。如果您不想要,请参考@sehe的答案。
答案 2 :(得分:1)
我会这样做:
编辑2
现已包括:
Parse
和TryParse
方法:)再次,请在 http://ideone.com/rukw4
上查看using System;
using System.Text;
using System.Text.RegularExpressions;
struct MyDate
{
public int? Year, Month, Day, Hour, Minute;
private static readonly Regex dtRegex = new Regex(
@"^(?<year>\d{4})?-(?<month>\d\d)?-(?<day>\d\d)?"
+ @"(?:T(?<hour>\d\d)?:(?<minute>\d\d)?)?$",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
public static bool TryParse(string input, out MyDate result)
{
Match match = dtRegex.Match(input);
result = default(MyDate);
if (match.Success)
{
if (match.Groups["year"].Success)
result.Year = Int32.Parse(match.Groups["year"].Value);
if (match.Groups["month"].Success)
result.Month = Int32.Parse(match.Groups["month"].Value);
if (match.Groups["day"].Success)
result.Day = Int32.Parse(match.Groups["day"].Value);
if (match.Groups["hour"].Success)
result.Hour = Int32.Parse(match.Groups["hour"].Value);
if (match.Groups["minute"].Success)
result.Minute = Int32.Parse(match.Groups["minute"].Value);
}
return match.Success;
}
public static MyDate Parse(string input)
{
MyDate result;
if (!TryParse(input, out result))
throw new ArgumentException(string.Format("Unable to parse MyDate: '{0}'", input));
return result;
}
public override string ToString()
{
return string.Format("{0:0000}-{1:00}-{2:00}T{3:00}:{4:00}", Year, Month, Day, Hour, Minute);
}
public static implicit operator MyDate(string input)
{
return Parse(input);
}
}
class Program
{
static void Main(string[] args)
{
foreach (var testcase in new [] {
"2011-11-07T11:17",
"-11-07T11:17",
"2011--07T11:17",
"2011-11-T11:17",
"2011-11-07T:17",
"2011-11-07T11:",
// extra:
"--T11:17", // (11:17 am, no date, only time)
"-11-07", // (november the 7th, no year, no time)
// failures:
"2011/11/07 T 11:17",
"no match" })
{
MyDate parsed;
if (MyDate.TryParse(testcase, out parsed))
Console.WriteLine("'{0}' -> Parsed into '{1}'", testcase, parsed);
else
Console.WriteLine("'{0}' -> Parse failure", testcase);
}
}
}
输出:
'2011-11-07T11:17' -> Parsed into '2011-11-07T11:17'
'-11-07T11:17' -> Parsed into '-11-07T11:17'
'2011--07T11:17' -> Parsed into '2011--07T11:17'
'2011-11-T11:17' -> Parsed into '2011-11-T11:17'
'2011-11-07T:17' -> Parsed into '2011-11-07T:17'
'2011-11-07T11:' -> Parsed into '2011-11-07T11:'
'--T11:17' -> Parsed into '--T11:17'
'-11-07' -> Parsed into '-11-07T:'
'2011/11/07 T 11:17' -> Parse failure
'no match' -> Parse failure
答案 3 :(得分:0)
如果您知道日期格式,则可以使用DateTime.ParseExact
...如果您需要与strtol
类似的内容,则可以使用Convert.ToInt32
。