4

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

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

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

所以,我不知道如何拆分这些字符串并存储在表的不同列中。请帮助我。

4

3 回答 3

11
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}- 匹配四位数字
    • (...)- 表示第一组
  • (.*)- 匹配其他所有内容并创建第二组
于 2013-10-30T15:05:52.970 回答
0

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

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));
    }
}

值得一提的是,这是一个扩展方法,可以在第 n 次出现 astring 时将字符串“分解”成两半:

public static class StringExtensions
{
    public static string[] BreakOnNthIndexOf(this string input, string value, int breakOn, StringComparison comparison)
    {
        if (breakOn <= 0)
            throw new ArgumentException("breakOn must be greater than 0", "breakOn");
        if (value == null) value = " ";  // fallback on white-space

        int slashIndex = input.IndexOf(value, comparison);
        if (slashIndex >= 0)
        {
            int slashCount = 1;
            while (slashCount < breakOn && slashIndex >= 0)
            {
                slashIndex = input.IndexOf(value, slashIndex + value.Length, comparison);
                if (slashIndex >= 0) slashCount++;
            }
            if (slashCount == breakOn)
            {
                return new[] { 
                    input.Substring(0, slashIndex), 
                    input.Substring(slashIndex + value.Length) 
                };
            }
        }
        return new[]{ input };
    }
}

以这种方式使用它:

string line1 = "9/23/2013/marking abandoned based on notes below/DB";
string line2 = "12/8/2012/I think the thid is string/SG";
string[] res1 = line1.BreakOnNthIndexOf("/", 3, StringComparison.OrdinalIgnoreCase);
string[] res2 = line2.BreakOnNthIndexOf("/", 3, StringComparison.OrdinalIgnoreCase);
于 2013-10-30T15:13:05.673 回答
0

使用 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);
}

或者你可以使用IndexOfandSubstring方法:

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);
    }
}
于 2013-10-30T15:16:19.307 回答