C# 读取包含由制表符分隔的数据的文本文件

发布于 2024-07-25 02:43:55 字数 1000 浏览 3 评论 0原文

我有一些代码:

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

它读取一个文本文件,其中包含由制表符分隔的数据,并将数据拆分为单独的单词。

我遇到的问题是,一旦数据被分离,列表中随机字符串的左侧和右侧仍然有大量的空白(事实上大多数都是这样)。 我无法修剪字符串,因为它只删除空格,从技术上讲,这不是空格。

有人对如何解决这个问题有任何想法吗?

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!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-08-01 02:43:55

我遇到的问题是,一旦数据被分离,列表中随机字符串的左侧和右侧仍然有大量的空白(事实上大多数都是这样)。 我无法修剪字符串,因为它只删除空格,从技术上讲,这不是空格。

听起来您的字符串中有非制表符空白字符,并且是制表符分隔的。

使用 String.Trim 应该可以很好地删除这些额外的字符。 如果由于某种原因,对每个单词执行 String.Trim 不起作用,您需要切换以找出额外的“字符”的组成部分,并使用此 String.Trim 重载

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.

故事未完 2024-08-01 02:43:55

你有这样的空白/制表符吗? “    你好 ” ?

修剪也删除空格和制表符

You have white space/tabs like this? "    Hello " ?

Trim remove white spaces and tabs too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文