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

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

问题描述

我有一些代码:

 public static void ReadTextFile()
    {
        string line;

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

                char[] delimiters = new char[] { '	' };
                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.

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

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天全站免登陆