用条件将字符串分解为数组字符串

时间:2014-09-12 07:49:16

标签: c# arrays string

我有一个类似于13/03/14 17:18:03 0035880 FINLAND 00:00:23 3 2.34的字符串。现在我将简要描述一下这个字符串是怎样的......它可以像

13/03/14 17:18:03 0035880 FINLAND DEN 00:00:23 3 2.34

13/03/14 17:18:03 0035880 FINLAND DEN MEN 00:00:23 3 2.34

13/03/14 17:18:03 0035880 FINLAND DEN MEN CHEN 00:00:23 3 2.34

现在在我的程序中,我必须根据空格将这些字符串分解为数组字符串。字符串的顺序除了FINLANDFINLAND DEN之外的位置都是相同的。现在根据我的要求我必须将FINLAND DEN MEN CHEN添加到一个数组元素中。

我没有得到如何将这四个不同的子字符串FINLAND DEN MEN CHEN添加到一个数组元素中,如果它出现在字符串中。

这是我的c#代码..

string currentLine;
string[] arrline = currentLine.Split(' ');

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

一种方法是

var wordsArray = arrLine.Where(str => str.All(Char.IsLetter)).ToList();

搜索所有字符都是字母的条目


根据您的评论,您将遵循以下

    var arrline = current.Split(' ')
                         .GroupBy(x => x.All(Char.IsLetter) ? "True" : x)
                         .Select(g => string.Join(" ", g))
                         .ToList();

结果

enter image description here

答案 1 :(得分:0)

您可以使用DateTime.TryParseExactSubstringRemove等字符串方法和TakeWhile(Char.IsDigit)等LINQ方法的组合。

例如,这个查询:

 string lines = @"13/03/14 17:18:03 0035880 FINLAND DEN 00:00:23 3 2.34
                  13/03/14 17:18:03 0035880 FINLAND DEN MEN 00:00:23 3 2.34
                  13/03/14 17:18:03 0035880 FINLAND DEN MEN CHEN 00:00:23 3 2.34";

var lineInfos = lines.Split(new[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)
    .Where(l => l.Trim().Length > 33)
    .Select(l => {
        string line = l.Trim();
        string Number = null;
        string Country = null;
        string Timespan = null;
        DateTime? dtNullable = null;
        DateTime dt;
        String datePart = line.Remove(17);
        if (DateTime.TryParseExact(datePart, "dd/MM/yy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
        {
            dtNullable = dt;
            // extract the number from the following digits after the date:
            Number = string.Join("", line.Substring(18).TakeWhile(Char.IsDigit));
            // take the timspan at the end with a fixed length of 15 characters
            int tsLength = line.Length - 15;
            Timespan = line.Substring( tsLength ); // take from end
            // take the country from the rest 
            Country = line.Substring(18 + Number.Length, line.Length - 15 - 18 - Number.Length);                      
            return new { Date = dtNullable, Number, Country, Timespan };
        }
        else
            return new { Date = dtNullable, Number, Country, Timespan };
    })
    .Where(x => x.Date.HasValue); // take only valid lines

foreach (var lInfo in lineInfos)
    Console.WriteLine(lInfo.ToString());

结果:

{ Date = 13.03.2014 17:18:03, Number = 0035880, Country =  FINLAND DEN , Timespan = 00:00:23 3 2.34 }
{ Date = 13.03.2014 17:18:03, Number = 0035880, Country =  FINLAND DEN MEN , Timespan = 00:00:23 3 2.34 }
{ Date = 13.03.2014 17:18:03, Number = 0035880, Country =  FINLAND DEN MEN CHEN , Timespan = 00:00:23 3 2.34 }

List<String[]>

List<String[]> result = lineInfos 
    .Select(x => new[]{ x.Date.ToString(), x.Number, x.Country, x.Timespan })
    .ToList();

答案 2 :(得分:0)

Regex.Split(currentLine, "(?<![a-zA-Z]) | (?![a-zA-Z])");

分隔符是一个空格符号,后面没有任何字母或后面没有任何字母。 因此,两个字母之间的空格在字母和数字匹配之间不匹配。