Come impostare trovati elenco parole i valori di una volta .NET?

0

Domanda

Ho una classe in quanto tale:

    public class cls_words : IEquatable<cls_words>
    {
        public int indx { get; set; }
        public string wordTxt { get; set; }
        public int wordIsFound { get; set; }

        public override string ToString()
        {
            return "ID: " + wordIsFound + "   Name: " + wordTxt;
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            cls_words objAsWord = obj as cls_words;
            if (objAsWord == null) return false;
            else return Equals(objAsWord);
        }
        public override int GetHashCode()
        {
            return wordIsFound;
        }

        public bool Equals(cls_words other)
        {
            if (other == null) return false;
            return (this.wordIsFound.Equals(other.wordIsFound));
        }
    }

Fondamentalmente la classe è una parola, e se sia o non è stato trovato in una ricerca.

Così ho creato una lista di questa classe in quanto tale:

List<cls_words> wordsIn = new List<cls_words>();

wordsIn.Add(new cls_words { indx= 1, wordTxt = "test", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 2, wordTxt = "the", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 3, wordTxt = "test", wordIsFound=0 });

Quindi quando faccio una ricerca elenco per vedere se contiene una parola, voglio impostare tutti wordIsFound i valori di 1, ove appropriato. Alcune parole nell'elenco potrebbe essere lo stesso.

Quindi qualcosa di simile

string wordSearch = "test";

if (wordsIn.Exists(x => x.wordTxt == wordSearch)) {

   //set all wordIsFound = 1 where word matches wordSearch 

}

Così come ho impostato wordIsFound a 1 su 1 ° e 3 ° elemento della lista (quelli che corrispondono a wordSearch ?

.net c# list match
2021-11-23 21:30:19
2
0
string wordSearch = "test";
 foreach (cls_words clsword in wordsIn) {
   if(cls_word.wordTxt == wordSearch) {
      clsword.wordIsFound = 1;
   }
}
2021-11-23 21:35:17

che è il migliore/unico modo? in un ciclo?
E.D.

thx per le risposte, ho pensato che ci potrebbe essere un modo più elegante di un ciclo. Per esempio, la linea: ` se (wordsIn.Esiste(x => x.wordTxt == wordSearch)) { `` ....controlla tutte le parole senza un loop. So che dietro le quinte si è a loop, ma ho pensato che ci potrebbe essere un codice simile per impostazione. Io non credo.
E.D.

@E. D. sì. C'è una funzione ForEach ma internamente fa lo stesso con meno prestazioni e la leggibilità:S
Leandro Bardelli
0

Più facile approccio potrebbe essere quello di utilizzare un HashSet con il tuo tipo (o anche solo un string).

HashSet<string> foundWords = new HashSet<string>();
HashSet<string> allWords = new HashSet<string>(); 
allWords.Add("test"); 
allWords.Add("apple");
allWords.Add("test"); // This will just "fail silently" if the word is already in the allWords HashSet.

// then to test:
string wordSearch = "test";
if (allWords.Contains(wordSearch)) foundWords.Add(wordSearch);

// you can even loop over your input of a very big string with something like:
string bigString = "this is a long string with many words";
int maxWords = 1000;
string[] allWordsToTest = bigString.Split(' ', maxWords);
foreach (string s in allWordsToTest)
    if (allWords.Contains(s)) foundWords.Add(s);

Ovviamente si sta andando a voler fare qualche trattamento (set le parole in modo uniforme, vuota il "trovato" prove, ecc), ma che deve iniziare.

2021-11-23 21:42:34

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................