I am trying to get a list of words (below) to be put into an array. I want each word to be in it's own index.
我试图得到一个单词列表(下面)放入一个数组。我希望每个单词都在它自己的索引中。
Here is my code that I have so far.
这是我到目前为止的代码。
string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWordsLine = line.Split(' ');
int BadWordArrayCount = 0;
foreach (string word in badWordsLine)
{
badWords[BadWordArrayCount] = word;
BadWordArrayCount = BadWordArrayCount + 1;
}
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
Here is the list of words I want to import.
这是我要导入的单词列表。
label
invoice
post
document
postal
calculations
copy
fedex
statement
financial
dhl
usps
8
notification
n
irs
ups
no
delivery
ticket
If someone could give me an example of how to get this to work, that would be a huge help.
如果有人能给我一个如何让它工作的例子,那将是一个巨大的帮助。
5 个解决方案
#1
4
Simplified code:
简化代码:
string badWordsFilePath = openFileDialog2.FileName.ToString();
string[] badWords = File.ReadAllLines(badWordsFilePath);
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#2
3
If every word starts on a new line then you do not need to create a for loop. The Split method will convert to an array for you.
如果每个单词都以新行开头,那么您不需要创建for循环。 Split方法将为您转换为数组。
string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWords = line.Split('\n');
#3
0
You are splitting on space, but there is a newline between each word. Split on newline instead:
你在空间上分裂,但每个单词之间有一个换行符。在换行符上拆分:
string[] badWordsLine = line.Split(new string[]{ Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Then you have to create the array to put the words in:
然后你必须创建数组以将单词放入:
badWords = new string[badWordsLine.Length];
However, to loop through a string array just to copy the strings to a string array seems pointless. Just assing the string array to the variable. Also, you forgot to close the stream reader, which is best taken care of with a using
block:
但是,循环一个字符串数组只是为了将字符串复制到字符串数组似乎毫无意义。只是将字符串数组赋予变量。此外,您忘记关闭流阅读器,最好用一个使用块来处理:
string badWordsFilePath = openFileDialog2.FileName.ToString();
string line;
using (StreamReader sr = new StreamReader(badWordsFilePath)) {}
line = sr.ReadToEnd();
}
badWords = line.Split(new string[]{ Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#4
0
Maybe try this modification? It allows on splitting on various white spaces.
也许尝试这个修改?它允许在各种白色空间上分裂。
string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWordsLine = line.Split(new string[] {" ", "\t", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
int BadWordArrayCount = 0;
foreach (string word in badWordsLine)
{
badWords[BadWordArrayCount] = word;
BadWordArrayCount = BadWordArrayCount + 1;
}
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#5
0
Do you have to use a StreamReader? If you do not have to, then this code is clearer (in my opinion).
你必须使用StreamReader吗?如果你没有,那么这个代码更清晰(在我看来)。
string text = File.ReadAllText(badWordsFilePath);
string[] words = Regex.Split(text, @"\s+");
If you're 100% certain that each word is on its own line and there are no empty lines, this might be overkill; and the File.ReadAllLines suggestion by @Ulugbek Umirov is simpler.
如果你100%确定每个单词都在它自己的行上并且没有空行,那么这可能是过度的;和@Ulugbek Umirov的File.ReadAllLines建议更简单。
#1
4
Simplified code:
简化代码:
string badWordsFilePath = openFileDialog2.FileName.ToString();
string[] badWords = File.ReadAllLines(badWordsFilePath);
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#2
3
If every word starts on a new line then you do not need to create a for loop. The Split method will convert to an array for you.
如果每个单词都以新行开头,那么您不需要创建for循环。 Split方法将为您转换为数组。
string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWords = line.Split('\n');
#3
0
You are splitting on space, but there is a newline between each word. Split on newline instead:
你在空间上分裂,但每个单词之间有一个换行符。在换行符上拆分:
string[] badWordsLine = line.Split(new string[]{ Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Then you have to create the array to put the words in:
然后你必须创建数组以将单词放入:
badWords = new string[badWordsLine.Length];
However, to loop through a string array just to copy the strings to a string array seems pointless. Just assing the string array to the variable. Also, you forgot to close the stream reader, which is best taken care of with a using
block:
但是,循环一个字符串数组只是为了将字符串复制到字符串数组似乎毫无意义。只是将字符串数组赋予变量。此外,您忘记关闭流阅读器,最好用一个使用块来处理:
string badWordsFilePath = openFileDialog2.FileName.ToString();
string line;
using (StreamReader sr = new StreamReader(badWordsFilePath)) {}
line = sr.ReadToEnd();
}
badWords = line.Split(new string[]{ Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#4
0
Maybe try this modification? It allows on splitting on various white spaces.
也许尝试这个修改?它允许在各种白色空间上分裂。
string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWordsLine = line.Split(new string[] {" ", "\t", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
int BadWordArrayCount = 0;
foreach (string word in badWordsLine)
{
badWords[BadWordArrayCount] = word;
BadWordArrayCount = BadWordArrayCount + 1;
}
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;
#5
0
Do you have to use a StreamReader? If you do not have to, then this code is clearer (in my opinion).
你必须使用StreamReader吗?如果你没有,那么这个代码更清晰(在我看来)。
string text = File.ReadAllText(badWordsFilePath);
string[] words = Regex.Split(text, @"\s+");
If you're 100% certain that each word is on its own line and there are no empty lines, this might be overkill; and the File.ReadAllLines suggestion by @Ulugbek Umirov is simpler.
如果你100%确定每个单词都在它自己的行上并且没有空行,那么这可能是过度的;和@Ulugbek Umirov的File.ReadAllLines建议更简单。