简体   繁体   English

如何修改已创建的列表?

[英]How can I modify a list which is already created?

I have a clients list that I need to access (from a .txt file) and to change any of its lines and (for ex Change telephone number) 我有一个客户列表,我需要访问(从.txt文件)并更改其任何行,以及(例如,更改电话号码)

I have already a code that opens a .txt file, reads from it, allows me to add new information and save it to the same file afterwards. 我已经有一个打开.txt文件,从中读取文件的代码,允许我添加新信息,然后将其保存到同一文件中。 The list, therefore, becomes updated. 因此,该列表将更新。 As well it allows me to search by keyword inside the list. 同样,它还允许我在列表中按关键字搜索。

   public static void ShowClientList() {

        string file =File.ReadAllText(@"C:\Users\Adminl\Documents\clients.txt");
        Console.WriteLine(file);
        Console.WriteLine();
    }

 public static void AddClient()
    {   Console.WriteLine("Civ");
        string civility = Console.ReadLine();                        
        Console.WriteLine("Name");
        string name = Console.ReadLine();                        
        Console.WriteLine("Surname");
        string surname = Console.ReadLine();                        
        Console.WriteLine("Age");
        string age = Console.ReadLine();                        
        Console.WriteLine("Telephone No");
        string telephone = Console.ReadLine();                       

        string appendText = civility +','+" "+ name + ',' + " " + surname + ',' + " " + age + ',' + " " + telephone + Environment.NewLine;        // This text is always added, making the file longer over time if its not deleted

        string path = @"C:\Users\Adminl\Documents\clients.txt";     // FILE that either exist or no


        File.AppendAllText(path, appendText);
    }

  public static void SearchClients()
    {

        string line;
        StreamReader clients = new StreamReader(@"C:\Users\Adminl\Documents\clients.txt");  // Read the file and display it line by line.  

            List<string> lines = new List<string>();


            while ((line = clients.ReadLine()) != null)
            {
                lines.Add(line);
            }

            Console.WriteLine("Please insert the criteria: ");
            string choose = Console.ReadLine();

            for (int i = 0; i < lines.Count; i++)
            {

                if (lines[i].ToUpper().Contains(choose.ToUpper()))           // search any part
                {
                    Console.WriteLine(lines[i]);
                }
            }

     }

I'd like to choose the line from the list (of clients) and be able to change it. 我想从(客户)列表中选择该行,并能够对其进行更改。 If possible, also give a unique number to every line. 如果可能,还为每行指定一个唯一的编号。 Sorry I am a total newbie, so please don't hit me too much. 对不起,我是一个新手,所以请不要打我太多。 Thanks a lot for the help! 非常感谢您的帮助!

You could get the particular index by matching the values using List.FindIndex method. 您可以通过使用List.FindIndex方法匹配值来获取特定索引。 In your code, you may find by matching the choose value you declared as below. 在您的代码中,可以通过匹配以下声明的select值来查找。

 string choose = Console.ReadLine();
 int index= lines.FindIndex(value => value== choose);

Also, you may change the line by directly passing the value through the index as below 另外,您可以通过将值直接传递给索引来更改行,如下所示

 lines[index] = "ModifiedLine";

For your another question "If possible, also give a unique number to every line." 对于您的另一个问题, “如果可能,请在每行中添加一个唯一的数字”。

Answer is that you may not give unique number to a line using List , instead you may try Dictionary as it has unique key which a list does not. 答案是您可能不会使用List给行赋予唯一编号,而是可以尝试使用Dictionary,因为它具有列表没有的唯一 Please find the declaration of using List and Dictionary 请找到使用列表和字典的声明

List 名单

List<string> lines = new List<string>();

Dictionary 字典

Dictionary<int, string> lines = new Dictionary<int, string>();

You can get more details about Dictionary in the link 您可以在链接中获取有关字典的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM