Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中拆分带日期的字符串#_C#_Asp.net - Fatal编程技术网

C# 如何在c中拆分带日期的字符串#

C# 如何在c中拆分带日期的字符串#,c#,asp.net,C#,Asp.net,我有带日期的字符串,我想用日期和字符串拆分它 例如: 我有这种类型的字符串数据 9/23/2013/marking abandoned based on notes below/DB 12/8/2012/I think the thid is string/SG 我想让它像 9/23/2013 marking abandoned based on notes below/DB 12/8/2013 I think the thid is string/SG 所以,我不知道如

我有带日期的字符串,我想用日期和字符串拆分它

例如: 我有这种类型的字符串数据

9/23/2013/marking abandoned based on notes below/DB
12/8/2012/I think the thid is string/SG
我想让它像

9/23/2013     marking abandoned based on notes below/DB
12/8/2013     I think the thid is string/SG
所以,我不知道如何拆分这些字符串并存储在表的不同列中。 请帮帮我

string[] vals = { "9/23/2013/marking abandoned based on notes below/DB",
                  "12/8/2012/I think the thid is string/SG" };

var regex = @"(\d{1,2}/\d{1,2}/\d{4})/(.*)";

var matches = vals.Select(val => Regex.Match(vals, regex));

foreach (var match in matches)
{
    Console.WriteLine ("{0}     {1}", match.Groups[1], match.Groups[2]);
}
印刷品:

9/23/2013     marking abandoned based on notes below/DB
12/8/2012     I think the thid is string/SG
(\d{1,2}/\d{1,2}/\d{4}/(.*)
分解为

  • (\d{1,2}/\d{1,2}/\d{4})
    • \d{1,2}
      -匹配任何一位或两位数字
    • /
      -与一个
      /
      符号匹配
    • \d{4}
      -与四位数匹配
    • (…)
      -表示第一组
  • (.*)
    -匹配所有其他内容并创建第二个组

也许对于纯字符串方法,第三个斜杠分隔日期和文本:

string line = "9/23/2013/marking abandoned based on notes below/DB";
int slashIndex = line.IndexOf('/');
if(slashIndex >= 0)
{
    int slashCount = 1;
    while(slashCount < 3 && slashIndex >= 0)
    {
        slashIndex = line.IndexOf('/', slashIndex + 1); 
        if(slashIndex >= 0) slashCount++;
    }
    if(slashCount == 3)
    {
        Console.WriteLine("Date:{0}   Text: {1}"
            , line.Substring(0, slashIndex)
            , line.Substring(slashIndex +1));
    }
}

使用LINQ的另一种方法是:

var inputs = new[]{
    "9/23/2013/marking abandoned based on notes below/DB",
    "12/8/2012/I think the thid is string/SG"
};

foreach (var item in inputs)
{
    int counter = 0;
    var r = item.Split('/')
        .Aggregate("", (a, b) =>
            a + ((counter++ == 3) ? "\t" : ((counter == 1) ? "" : "/")) + b);
    Console.WriteLine(r);
}
或者,您可以使用
索引of
子字符串
方法:

foreach (var item in inputs)
{
    var lastPos = 
        item.IndexOf('/', 
            1 + item.IndexOf('/', 
                1 + item.IndexOf('/')));
    if (lastPos != -1)
    {
        var r = String.Join("\t", 
            item.Substring(0, lastPos), 
            item.Substring(lastPos + 1, item.Length - lastPos - 1));
        Console.WriteLine(r);
    }
}

-这在将来可能会有所帮助,但是,所有这些字符串都来自列表中的数据库,所以如何拆分呢it@Nikunj您可以通过ORM或ado.net将它们加载到内存中,然后进行处理吗?更正了方法调用“
Regex.Match(vals,Regex)
”附近的键入错误;以前不是val,而是val@我能帮上忙吗非常感谢:)天哪,这是一个很老的答案——六年前。等等,应该有一个
val
,注意
val
参数名在lambda
val=>Regex.Match(val,Regex)
var inputs = new[]{
    "9/23/2013/marking abandoned based on notes below/DB",
    "12/8/2012/I think the thid is string/SG"
};

foreach (var item in inputs)
{
    int counter = 0;
    var r = item.Split('/')
        .Aggregate("", (a, b) =>
            a + ((counter++ == 3) ? "\t" : ((counter == 1) ? "" : "/")) + b);
    Console.WriteLine(r);
}
foreach (var item in inputs)
{
    var lastPos = 
        item.IndexOf('/', 
            1 + item.IndexOf('/', 
                1 + item.IndexOf('/')));
    if (lastPos != -1)
    {
        var r = String.Join("\t", 
            item.Substring(0, lastPos), 
            item.Substring(lastPos + 1, item.Length - lastPos - 1));
        Console.WriteLine(r);
    }
}