Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向文本文件添加新的唯一字符串_C#_String_Text Files_Unique - Fatal编程技术网

C# 如何向文本文件添加新的唯一字符串

C# 如何向文本文件添加新的唯一字符串,c#,string,text-files,unique,C#,String,Text Files,Unique,我有一个文本文件,其中包含几行文字,例如 cards door lounge dog window 我想在列表中添加一个新词,条件是它在列表中不存在。例如,我想添加风或车 我使用File.ReadAllText(@“C:\Temp.txt”).Contains(word) 但问题是窗口包含风和卡包含汽车 有什么方法可以唯一地比较它吗?使用File.ReadLine()并用String.equals()检查,不要查找子字符串。大概是这样的: while(!reader.EndOfFile0 {

我有一个文本文件,其中包含几行文字,例如

cards
door
lounge
dog
window
我想在列表中添加一个新词,条件是它在列表中不存在。例如,我想添加

我使用
File.ReadAllText(@“C:\Temp.txt”).Contains(word)
但问题是
窗口
包含
包含
汽车

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

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

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

如果您没有大型文件,可以将其读取到内存中,并像处理任何阵列一样进行处理:

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
}

您应该通过Regex match进行匹配,以便匹配一个精确的工作,我在下面将其设置为不区分大小写

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;
    }
}

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

您可以执行以下操作

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);
}

我正要写这个方法!:呵呵,是你先写的,所以+1:)它行得通,但我想到了另一个问题,如果每个单词旁边都有一个属性呢?例如
卡$29
门$32
。在比较时,如何忽略该属性?在这种情况下,需要更复杂的解析。如果格式是始终有一个$符号和一个数字,则可以查找
$
符号,并在进行匹配时删除从该符号开始的所有内容