从文本文件中拆分字符串

时间:2010-09-16 09:32:12

标签: c# string split

我在文本文件“test”中有以下字符串

Table Name.type
Market Drinks.tea

我wana拆分字符串,以便得到以下输出

ObjectName = Table    AttributeName = Name    Attribute Type =type
ObjectName = Market   AttributeName = Drinks  Attribute Type =tea

这是我的代码

            string[] lines = File.ReadAllLines(@"d:\test.txt");
            int i = 0;
            var items = from line in lines
                        where i++ != 0
                        select new{
                            objectName = line.Split(new char[] {' '})[0],
                            attrName = line.Split(new char[]{'.'})[1],
                            attrType = line.Split(new char[] { ' ' })[2]
                        };
            foreach (var item in items)
            {
                Console.WriteLine("ObjectName = {0}, AttributeName = {1}, Attribute Type = {2}",
                    item.objectName, item.attrName, item.attrType);
            }

我越过界限异常了。

PS:文本文件中字符串末尾没有空格我只想测试一个字符!

6 个答案:

答案 0 :(得分:4)

您不需要new char[] { ... }周围因为String.Split()使用params

要修复索引越界,select的最后部分应变为:

        attrType = line.Split(' ', '.' )[2]

修改

感谢@Kobi,let只允许你进行一次拆分,当你有很多行和/或列时,这是一个很大的改进。

var items = from line in lines
            where i++ != 0
            let words = line.Split(' ', '.')
            select new
            {
                objectName = words[0],
                attrName = words[1],
                attrType = words[2]
            };

旧答案

您可以对所有3个部分使用相同的分割,使其更容易阅读:

       select new{
             objectName = line.Split(' ', '.' )[0],
             attrName   = line.Split(' ', '.' )[1],
             attrType   = line.Split(' ', '.' )[2]
        };

答案 1 :(得分:2)

使用更健壮的正则表达式:

        static void Main()
    {

        const string PATTERN = @"^([\w]+)\s+([\w]+)\.(\w+)";
        const string TEXT = "Table Name.type\r\nMarket Drinks.tea";

        foreach (Match match in Regex.Matches(TEXT, PATTERN, RegexOptions.Multiline))
        {
            Console.WriteLine("ObjectName = {0}   AttributeName = {1}  Attribute Type ={2}",
                match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value);
        }

    }

输出:

ObjectName = Table   AttributeName = Name  Attribute Type =type
ObjectName = Market   AttributeName = Drinks  Attribute Type =tea

答案 2 :(得分:1)

在分割部分,您应该这样做(前提是您确定输入的格式正确):

attrName = line.Split(' ')[1].Split('.')[0],
attrType = line.Split(' ')[1].Split('.')[1]

答案 3 :(得分:1)

界限在这一行 - attrType = line.Split(new char[] { ' ' })[2]

你的attrType应为= line.Split(new char[] { '.' } )[1];

attrName应为= line.Split(new char[] {' '})[1].Split(new char[] {'.'})[0]

正如Henk Holterman所说,你不需要在分裂中使用新的char [],所以你的行将是 -

attrType = line.Split('.')[1];
attrName = line.Split(' ')[1].Split('.')[0];

答案 4 :(得分:0)

这里的回复是所需输出的正确答案

objectName = line.Split(' ')[0],
attrName = line.Split(' ')[1].Split('.')[0],
attrType = line.Split('.')[1]

答案 5 :(得分:0)

如果你想以这种方式做,只需使用Regex它更灵活

const string pattern = @"(?<objectName>\w+)\s(?<attrName>\w+)\.(?<attrType>\w+)";

string[] lines = File.ReadAllLines(@"e:\a.txt");
var items = from line in lines
            select new
            {
                objectName = Regex.Match(line, pattern).Groups["objectName"].Value,
                attrName = Regex.Match(line, pattern).Groups["attrName"].Value,
                attrType = Regex.Match(line, pattern).Groups["attrType"].Value
            };

foreach (var item in items.ToList())
{
    Console.WriteLine("ObjectName = {0}, AttributeName = {1}, Attribute Type = {2}",
        item.objectName, item.attrName, item.attrType);
}