問(wèn)題描述
我有一些代碼:
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();
}
它讀入一個(gè)包含由制表符分隔的數(shù)據(jù)的文本文件,并將數(shù)據(jù)拆分為單獨(dú)的單詞.
It reads in a text file that contains data delimited by tabs and splits the data into separate words.
我遇到的問(wèn)題是,一旦數(shù)據(jù)被分離,列表中隨機(jī)字符串的左右兩側(cè)仍然有大量空白(事實(shí)上大多數(shù)都有).我無(wú)法修剪字符串,因?yàn)樗粫?huì)刪除空格,從技術(shù)上講,這不是空格.
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.
任何人對(duì)如何解決這個(gè)問(wèn)題有任何想法!?
Anyone got any ideas on how to get round this problem!?
推薦答案
我遇到的問(wèn)題是,一旦數(shù)據(jù)被分離,列表中隨機(jī)字符串的左右兩側(cè)仍然有大量空白(事實(shí)上大多數(shù)都有).我無(wú)法修剪字符串,因?yàn)樗粫?huì)刪除空格,從技術(shù)上講,這不是空格.
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.
聽起來(lái)你的字符串中有非制表符空白字符,并且是制表符分隔的.
It sounds like you have non-tab whitespace characters in your string, as well as being tab delimited.
使用 String.Trim 應(yīng)該可以很好地刪除這些多余的字符.如果由于某種原因,對(duì)每個(gè)單詞執(zhí)行 String.Trim 不起作用,您需要切換以找出額外的字符"是由什么組成的,并使用這個(gè) 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.
這篇關(guān)于C# 讀取包含由制表符分隔的數(shù)據(jù)的文本文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!