我一直在编码存储学生姓名和年龄的程序(名字,姓氏,年龄在.txt文件中)。我现在正在制作“删除学生”部分,并且当我希望用户选择要删除的名称(通过切断文件名的.txt扩展名打印)时,它不会替换“.txt”部分一无所有。
的代码是:替换列表中的字符串C#
string inputSel; // Selection string for delete
Console.WriteLine(" -- Deleting Grade {0} -- ", grade);
Console.WriteLine("- Enter a student name to delete: ");
foreach (string file in fileNames)
{
file.Replace(".txt", "");
studentNames.Add(file);
Console.WriteLine(file); // debug
}
foreach (string name in studentNames)
{
Console.Write("{0}\t", name);
}
Console.WriteLine();
Console.Write("> ");
inputSel = Console.ReadLine();
,其中文件名是List<string>
,正是因为这个代码是在方法的参数studentNames也是List<string>
,它存储的名称(不.txt文件名)。但由于某些原因,它仍然会用.txt打印名称。长话短说,它不会取代".txt"
与""
。