2

I have a text file contains several lines with words, for example like this

cards
door
lounge
dog
window

I want to add a new word into that list with the condition that it does not already exist in the list. For example I want to add wind or car

I use File.ReadAllText(@"C:\Temp.txt").Contains(word) But the problem is window contains wind and cards contain car

Is there any way to compare it uniquely?

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Hien Tran
  • 1,443
  • 2
  • 12
  • 19

4 Answers4

3

If you have not a huge file, you can read it to memory and process it like any array:

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
}
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • I was about to write this method! :D hehehe, you wrote it first, so +1 :) – Afzaal Ahmad Zeeshan Sep 14 '14 at 06:16
  • It works, but I came up with another problem, what if there is an attribute next to each word? For example `cards $29` `door $32`. How can you ignore the attribute when comparing? – Hien Tran Sep 14 '14 at 06:17
  • 1
    In this case more complex parsing is required. If the format is that there is always a $ sign and a number, you can look for the `$` symbol and remove everything starting from this symbol when doing matching – dotnetom Sep 14 '14 at 06:20
2

Use File.ReadLine() and check for with String.equals(), dont look for substrings. Something Like this:

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

You should do through Regex match so that you are matching an exact work, and I have made this below as case insensitive.

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

Although I am reading an entire file and writing an entire file back. You can improve the performance by only writing one word rather a whole file again.

codebased
  • 6,945
  • 9
  • 50
  • 84
0

you can do something like

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);
}
Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
Ateeq
  • 797
  • 2
  • 9
  • 27