C#读取文本文件包含数据的分隔由制表符 [英] C# Read Text File Containing Data Delimited By Tabs

查看:2137
本文介绍了C#读取文本文件包含数据的分隔由制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码:

 公共静态无效ReadTextFile()
{
串线;

//读取文件并逐行显示。
使用(StreamReader的文件=新的StreamReader(@C:\Documents和Settings\Administrator\Desktop\snpprivatesellerlist.txt))
{
,而((行=文件.ReadLine())= NULL)
{

的char [] =分隔符字符新[] {'\t'}!;
的String [] =零件l​​ine.Split(分隔符,StringSplitOptions.RemoveEmptyEntries);
的for(int i = 0; I< parts.Length;我++)
{

Console.WriteLine(部分由[i]);
sepList.Add(部分由[i]);

}

}

file.Close();
}
//暂停屏幕。
到Console.ReadLine();
}



据读取包含由制表符分隔的数据,并会将数据的文本文件成单独的词语的



我的问题是,一旦数据已被分离出来,它仍然有在左侧的白色空间大量和右侧上在随机串列表(逸岸大多做)。我不能剪裁字符串,因为它不仅能消除白色的空间,在技术上这不是空白。



任何人有关于如何克服这个问题的任何想法! ?


解决方案

我的问题是,一旦数据被分开,它仍然有大量的量的列表中(逸岸大多做)对随机串的左侧和右侧的空白。我不能剪裁字符串,因为它不仅能消除白色的空间,在技术上这不是空白。




这听起来像你具有非标签空白字符在你的字符串,以及被制表符分隔。



使用String.Trim应该可以正常工作,以消除这些额外的字符。如果由于某种原因,在做String.Trim上的每一个字不工作,你需要切换到找出额外的字符是由,并使用此的的microsoft.com/en-us/library/d4tt83f9.aspx\">overload。


I have some code:

 public static void ReadTextFile()
    {
        string line;

        // Read the file and display it line by line.
        using (StreamReader file = new StreamReader(@"C:\Documents and Settings\Administrator\Desktop\snpprivatesellerlist.txt"))
        {
            while ((line = file.ReadLine()) != null)
            {

                char[] delimiters = new char[] { '\t' };
                string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < parts.Length; i++)
                {

                     Console.WriteLine(parts[i]);
                     sepList.Add(parts[i]);

                }

            }

            file.Close();
        }
        // Suspend the screen.
        Console.ReadLine();     
    }

It reads in a text file that contains data delimited by tabs and splits the data into separate words.

The problem I have is that once the data has been separated, it still has massive amounts of white space on the left and right sides on random strings in the list (Infact most of them do). I can't trim the string because it only removes white space, and technically this isn't white space.

Anyone got any ideas on how to get round this problem!?

解决方案

The problem I have is that once the data has been separated, it still has massive amounts of white space on the left and right sides on random strings in the list (Infact most of them do). I can't trim the string because it only removes white space, and technically this isn't white space.

It sounds like you have non-tab whitespace characters in your string, as well as being tab delimited.

Using String.Trim should work fine to remove these extra characters. If, for some reason, doing String.Trim on each word is not working, you'll need to switch to find out what the extra "characters" are comprised of, and using this overload of String.Trim.

这篇关于C#读取文本文件包含数据的分隔由制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆