【问题标题】:How to add a new unique string to text file如何将新的唯一字符串添加到文本文件
【发布时间】:2014-09-14 06:01:29
【问题描述】:

我有一个文本文件包含几行单词,例如这样

cards
door
lounge
dog
window

我想在该列表中添加一个新词,条件是它在列表中不存在。比如我想加wind或者car

我使用File.ReadAllText(@"C:\Temp.txt").Contains(word) 但问题是window 包含windcards 包含car

有什么方法可以唯一比较吗?

【问题讨论】:

    标签: c# string text-files unique


    【解决方案1】:

    如果你没有一个很大的文件,你可以将它读入内存并像任何数组一样处理它:

    var lines = File.ReadAllLines(@"C:\Temp.txt");
    if(lines.Any(x=>x == word)
    {
        //There is a word in the file
    }
    else
    {
        //Thee is no word in the file
    }
    

    【讨论】:

    • 我正要写这个方法! :D 呵呵呵呵,你先写的,所以+1 :)
    • 可以,但是我又想出了一个问题,如果每个单词旁边都有一个属性怎么办?例如cards $29door $32。比较时如何忽略属性?
    • 在这种情况下需要更复杂的解析。如果格式是总是有$符号和数字,你可以查找$符号,匹配时删除从这个符号开始的所有内容
    【解决方案2】:

    使用 File.ReadLine() 并使用 String.equals() 检查,不要寻找子字符串。像这样的:

    while(!reader.EndOfFile0
    {
          if(String.Compare(reader.ReadLine(),inputString, true) == 0)
          {
                //do your stuf here
          }
    }
    

    【讨论】:

      【解决方案3】:

      你应该通过正则表达式匹配来匹配一个精确的工作,我在下面做了这个不区分大小写。

      using ConsoleApplication3;
      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      using System.Text.RegularExpressions;
      
      public static class Program
      {
      
          private static void Main(string[] args)
          {
      
              // Read the file and display it line by line.
              System.IO.StreamReader file =
                 new System.IO.StreamReader("c:\\temp\\test.txt");
              var line = string.Empty;
      
              string fileData = file.ReadToEnd();
              file.Close();
      
              fileData = "newword".InsertSkip(fileData);
      
              StreamWriter fileWrite = new StreamWriter(@"C:\temp\test.txt", false);
              fileWrite.Write(fileData);
              fileWrite.Flush();
              fileWrite.Close();
      
          }
      
          public static string InsertSkip(this string word, string data)
          {
              var regMatch = @"\b(" + word + @")\b";
              Match result = Regex.Match(data, regMatch, RegexOptions.Singleline | RegexOptions.IgnoreCase);
              if (result == null || result.Length == 0)
              {
                  data += Environment.NewLine + word;
              }
              return data;
          }
      }
      

      虽然我正在读取整个文件并写回整个文件。您可以通过只写一个单词而不是再次写整个文件来提高性能。

      【讨论】:

        【解决方案4】:

        你可以做类似的事情

        string newWord = "your new word";
        string textFile = System.IO.File.ReadAllText("text file full path");
        if (!textFile.Contains(newWord))
        { 
            textFile = textFile + newWord;
            System.IO.File.WriteAllText("text file full path",textFile);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-07
          • 2014-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          • 1970-01-01
          相关资源
          最近更新 更多