我有一些代码:
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(); }
它读入一个包含由制表符分隔的数据的文本文件,并将数据分割成单独的单词.
我遇到的问题是,一旦数据被分离,它在列表中的随机字符串的左侧和右侧仍然具有大量的空白(其中大部分都是这样).我不能修剪字符串,因为它只会删除空白,而且技术上这不是空白.
任何人都有任何想法如何解决这个问题!
解决方法
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.
这听起来像您的字符串中有非标签空格字符,以及分隔符.
使用String.Trim应该可以正常的删除这些额外的字符.如果由于某种原因,在每个单词上执行String.Trim都不起作用,则需要切换以查找额外的“字符”由哪个组成,并使用此overload of String.Trim.