O
O
Oposum2015-12-05 20:10:34
C++ / C#
Oposum, 2015-12-05 20:10:34

How, when reading from a file of an array of strings, to discard and ignore one of the occurrences?

Hello everyone, I'm confused and can't solve the following:
there is a text file that contains the lines:

one two three ten lot soap test
five good lot test bread seven one
...

that is, the size of the array is the same in the line - 7, but in the future I need to discard only one word, and how to forget that I have a size of 7.
For example, the word "many", in the first line it has the index [4] and in the second line [2] - I need to ignore this word and assume that there is another word at this index, in the first line "soap", and in the second line "test". That is - do not remove all occurrences, but simply ignore it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sumor, 2015-12-05
@Sumor

var line = reader.ReadLine();
var words = line.Split(" ".ToCharArray()).ToList();
words.RemoveAt(4);
var result = string.Join(" ", words);

A
Anton, 2015-12-06
@MoonMaster

If you are reading the file line by line and want each word to be unique? then why not look towards collections. I think Set will do here about Collections C#

O
Oposum, 2015-12-06
@Oposum

static void Main()
    {
        string[] lines = System.IO.File.ReadAllLines(@"e:\temp\test.txt");
        List<string> result = new List<string>();
        foreach (string line in lines)
        {
            string[] split = line.Split(new Char[] { ' ' });
            foreach (string spl in split)
            {
                result.Add(spl);
                result.Remove("много");
            }
            foreach (string res in result)
            {
                Console.WriteLine(res + " - " + result.Count);
            }
        }
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }

now I have all the contents of the test.txt file listed! and I need to enter only the contents of one line so that the dimension of the list is one less than the original line

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question