을 설정하는 방법을 발견되는 모든 단어 목록 값은 한 번에습니다.NET?

0

질문

가 등과 같은:

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

기본적으로 클래스는 단어가 있는지 여부와 상관없이에서 발견되었습니다.

그래서 내가 목록을 만들은 이 클래스의 이와 같:

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

그 때 나는 검색 목록이 포함된 경우는 단어,나는 설정하려는 모든 wordIsFound 값을 1 고 있습니다. 어떤 단어 목록에서도 동일합니다.

그래서 뭔가

string wordSearch = "test";

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

   //set all wordIsFound = 1 where word matches wordSearch 

}

그래서 내가 어떻게 설정 wordIsFound1 에서 1,제 3 항목(목록에서 일치하는 사람을 익?

.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

는 최/유일한 방법은? 루프에서?
E.D.

thx 에 대한 답변,나이 있을 것이라고 생각했 더 우아한 방법으로 반복입니다. 예를 들어,라인:``만약(wordsIn.이 존재하(x=>x.wordTxt==성)){``....검사는 모든 단어가지 않고 있습니다. 난 뒤에서 그것을 반복지만,저는 있을 것이라고 생각했 유사한 코드에 대한 설정입니다. 나는 생각하지 않습니다.
E.D.

@E.D. 그렇습니다. 가 ForEach 기능 그러나 내부적으로는 같은 덜 성능 및 가독성:S
Leandro Bardelli
0

쉽게 접근 방식도 사용될 수 있 HashSet 와 함께 당신의 유형(또는 그냥 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);

분명히 당신이 가고 싶은 일부 처리(설정된 단어를 일관성 있는 경우,빈"찾을 설정한"사 trials,etc.),을 해야 하지만 당신은 시작합니다.

2021-11-23 21:42:34

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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