C# C IF语句不排除字符串
所以问题是:我想从文件中读取并排除某些我没有经验的字符串,所以我决定使用IF-So,我想排除字符串Name、facultyNumber等。但出于某种原因,它不排除它们?我试图从if语句中删除空格C# C IF语句不排除字符串,c#,if-statement,readfile,C#,If Statement,Readfile,所以问题是:我想从文件中读取并排除某些我没有经验的字符串,所以我决定使用IF-So,我想排除字符串Name、facultyNumber等。但出于某种原因,它不排除它们?我试图从if语句中删除空格 public void read() { string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", En
public void read()
{
string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", Encoding.Default);
double[] gread = new double[] { 1, 2, 3 };
List<string> texts = new List<string>();
int i= 0;
int z = 0;
foreach (string text in textes)
{
string[] words = textes[i].Split(' ');
for (int j = 0; j < words.Length; j++)
{
if (!words[j].Equals(" Name: ") || !words[j].Equals(" FacultyNumber: ") || !words[j].Equals(" Grades: ") || !words[j].Equals(" AverageGrade: "))
{
texts.Add(words[j]);
}
}
// for (int j = 2; j < texts.Count; j++)
// {
// gread[z] = Convert.ToDouble(texts[j]);
// }
// addStudent(texts[0], Convert.ToInt32(texts[1]), gread);
for (int j = 0; j < 40; j++)
{
Console.WriteLine(texts[j]);
}
i++;
}
您应该满足所有条件(&&T)。由于| |的偏好是从左到右,如果最左的条件为真,则不考虑其他条件。因此,如果一个词通过了这部分条件!words[j]。等于Name:,它将通过整个条件。因此,如果有FacultyNumber或Grades这样的词通过了第一个条件,那么您将无法获得指定的结果。您可以按如下方式修改代码:
if (!(words[j].Equals(" Name: ") ||
words[j].Equals(" FacultyNumber: ") ||
words[j].Equals(" Grades: ") ||
words[j].Equals(" AverageGrade: ")))
您可以使用如下内容:
var excluded = new[] { "Name:", "FacultyNumber:", "Grades:", "AverageGrade:" };
foreach (string text in textes)
{
string[] words = textes[i].Split(' ');
for (int j = 0; j < words.Length; j++)
{
if (!excluded.Contains(words[j]))
{
texts.Add(words[j]);
}
}
}
我对编程相当陌生,不知道使用string.Replace方法是否有任何不利之处,但我会这样做
public void read()
{
string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", Encoding.Default);
List<string> texts = new List<string>();
for (int i = 0; i < textes.Lenght; i++)
{
texts.Add(textes[i].Replace(" Name: ", ""));
texts.Add(textes[i].Replace(" FacultyNumber: ", ""));
texts.Add(textes[i].Replace(" Grades: ", ""));
texts.Add(textes[i].Replace(" AverageGrade: ", ""));
}
foreach (string text in texts)
{
Debug.WriteLine(text);
}
}
我得到的错误是因为它传递了我声明不想在ifRemove中传递的字符串-删除空格!单词[j]。等于名称:应该是!words[j].EqualsName:正确答案是Zohar Peled的上述评论和OmG的答案。拆分会删除单词中的空格,被否定的| |不正确。任何字符串都不能同时等于Name:和FacultyNumber!所以整个条件都是真的。@XouDo你是对的。这是一个小错误。它被更新了。这意味着每个输入行重复4次,不同的关键字被删除。在同一字符串上执行所有替换,以删除所有。但是你是对的,这是一个删除关键字的解决方案,如问题中所述
string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", Encoding.Default);
var excluded = new[] { "Name:", "FacultyNumber:", "Grades:", "AverageGrade:" };
var texts = textes.SelectMany(x => x.Split(' ').Where(y => !excluded.Contains(y)));
foreach(string word in texts)
{
Console.WriteLine(word);
}
public void read()
{
string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", Encoding.Default);
List<string> texts = new List<string>();
for (int i = 0; i < textes.Lenght; i++)
{
texts.Add(textes[i].Replace(" Name: ", ""));
texts.Add(textes[i].Replace(" FacultyNumber: ", ""));
texts.Add(textes[i].Replace(" Grades: ", ""));
texts.Add(textes[i].Replace(" AverageGrade: ", ""));
}
foreach (string text in texts)
{
Debug.WriteLine(text);
}
}