Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. Wie kann ich 2 Strings überprüfen ob sie ein gleiches Wort enthalten?
QtWS25 Last Chance

Wie kann ich 2 Strings überprüfen ob sie ein gleiches Wort enthalten?

Scheduled Pinned Locked Moved Unsolved German
4 Posts 3 Posters 553 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Urbi
    wrote on last edited by Urbi
    #1

    Hallo, ich möchte in einer If Bedingung auf einfache Weise überprüfen, ob in 2 Strings jeweils (mindestens) 1 Wort identisch ist.
    Beipiel:
    String1 = "Wort1 Wort2 Wort3 Wort4 Wort5"
    String2 = "Wort3 Wort6 Wort8"
    Das Ergebnis soll Wahr sein, weil Wort3 ja in beiden Strings vorkommt.
    Ich suche also sowas wie If sting1.contains("ein Wort aus String2")
    Wie kann ich das realisieren?

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • U Urbi

      Hallo, ich möchte in einer If Bedingung auf einfache Weise überprüfen, ob in 2 Strings jeweils (mindestens) 1 Wort identisch ist.
      Beipiel:
      String1 = "Wort1 Wort2 Wort3 Wort4 Wort5"
      String2 = "Wort3 Wort6 Wort8"
      Das Ergebnis soll Wahr sein, weil Wort3 ja in beiden Strings vorkommt.
      Ich suche also sowas wie If sting1.contains("ein Wort aus String2")
      Wie kann ich das realisieren?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Urbi
      In Englisch:

      1. Split each string into its words (QString::split()).
      2. Store these words in 2 separate lists (QStringList).
      3. Foreach word1 in list1: Foreach word2 in list2: if word1 == word2 return found

      You can use list2.contains(word1) (QStringList::contains()).

      To make it fast, sort the list2 or store its words in a QMap so that you can look up to see if a word is there efficiently. Only needed if 100s of words.

      Muss ich ins Deutsche übersetzen ? :)

      1 Reply Last reply
      2
      • U Urbi

        Hallo, ich möchte in einer If Bedingung auf einfache Weise überprüfen, ob in 2 Strings jeweils (mindestens) 1 Wort identisch ist.
        Beipiel:
        String1 = "Wort1 Wort2 Wort3 Wort4 Wort5"
        String2 = "Wort3 Wort6 Wort8"
        Das Ergebnis soll Wahr sein, weil Wort3 ja in beiden Strings vorkommt.
        Ich suche also sowas wie If sting1.contains("ein Wort aus String2")
        Wie kann ich das realisieren?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @Urbi
        da ich gerade am prokrastinieren bin...

        bool mindestensEinWortIstIdentisch( const QString &string1, const QString &string2, const QString &trennzeichen = QString(QChar::Space))
        {
            const auto liste = string2.splitRef(trennzeichen);
            for(const QStringRef &element : liste){
                if(string1.count(element) > 0)
                    return true;
            }
            return false;
        }
        
        #include <QDebug>
        
        #include <QApplication>
        int main(int argc, char *argv[])
        {
        //    QApplication a(argc, argv);
        
            QString String1 = "Wort1 Wort2 Wort3 Wort4 Wort5";
            QString String2 = "Wort3 Wort6 Wort8";
        
            qDebug() << "Gefunden:" << mindestensEinWortIstIdentisch(String1, String2);
        
        //    return a.exec();
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          Urbi
          wrote on last edited by
          #4

          Danke für eure Antworten. Werde ich bei Gelegenheit mal ausprobieren. Inzwischen habe ich das konkrete Problem auf eine ganz andere Art gelöst, sodass ich besagte If Abfrage nicht benötige. Benötigt noch einmal weniger Rechenzeit.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved