I have the following strings in a text file "test"
Table Name.type
Market Drinks.tea
I wana split the strings so that I get the following output
ObjectName = Table    AttributeName = Name    Attribute Type =type
ObjectName = Market   AttributeName = Drinks  Attribute Type =tea
here is my code
            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);
            }
I'm getting an out of boundaries exception.
PS: there are no spaces at the end of the strings in the text file I just wanted to test a character!
You don't need the new char[] { ... } surrounding because String.Split() uses params 
To fix the index-out-of-bounds, the last part of the select should become:
        attrType = line.Split(' ', '.' )[2]
Edit
And thanks to @Kobi, a let will let you do the Split just once, a great improvement when you have many rows and/or columns. 
var items = from line in lines
            where i++ != 0
            let words = line.Split(' ', '.')
            select new
            {
                objectName = words[0],
                attrName = words[1],
                attrType = words[2]
            };
Old answer
You can use the same Split for all 3 parts, making it a little easier to read:
       select new{
             objectName = line.Split(' ', '.' )[0],
             attrName   = line.Split(' ', '.' )[1],
             attrType   = line.Split(' ', '.' )[2]
        };
Use regular expressions which is more robust:
        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);
        }
    }
Outputs:
ObjectName = Table   AttributeName = Name  Attribute Type =type
ObjectName = Market   AttributeName = Drinks  Attribute Type =tea
On the splitting part, you should do it like this (provided you are sure your input is in the correct format):
attrName = line.Split(' ')[1].Split('.')[0],
attrType = line.Split(' ')[1].Split('.')[1]
The out of bounds is on this line - attrType = line.Split(new char[] { ' ' })[2] 
your attrType should be = line.Split(new char[] { '.' } )[1];
attrName should be = line.Split(new char[] {' '})[1].Split(new char[] {'.'})[0] 
As Henk Holterman has said, you dont need to use new char[] inside split so your lines would be -
attrType = line.Split('.')[1];
attrName = line.Split(' ')[1].Split('.')[0];
thx to the replies here is the right answer for the desired output
objectName = line.Split(' ')[0],
attrName = line.Split(' ')[1].Split('.')[0],
attrType = line.Split('.')[1]
if you want to do in this way just use Regex its more flexible
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);
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论