QML If statement matching strings in a variable ?



  • I am searching a way to match words within a string message I received from websockets which is stored in a variable using an IF statement. so if the words within a string match to perform an action. I am not sure on how to go about and use an regular expressions in QML but I have tried using the following which does not work:

    var post = "This is a string received from websockets"
    textbox.text = post;
    if (textbox.text[1] === "This" && textbox.text[3] === "string") {
            somebutton.visisble=true;
    }
    

    I've also tried this:

    var post = "This is a string received from websockets"
    textbox.text = post;
    var strfind = textbox.text.indexOf("This")
    var strfind2 = textbox.text.indexOf("String")
    if (textbox.text.indexOf("This") === strfind && textbox.text.indexOf("String") === strfind2) {
            somebutton.visisble=true;
    }
    

  • Moderators

    @AexAcad
    In your first method is textbox.text an array ? If yes, then use split method to convert string to array.
    The second method should work because the LHS and RHS in the condition are exactly the same. The only problem is see there is visisble. Is that a typo here ? It will be undefined in your code and thus the operation will fail.



  • @p3c0

    textbox.text is not an array, and the second condition fails even if the conditions are the same,
    If i use the whole statement like:

    if (textbox.text === "This is a string received from websockets") {
    somebutton.visible = true
    }
    
    

    It works but that's exactly what i am not trying to do is use the whole statement. I am trying use Keywords within the string as I want to use the same method for other strings received which do not always have the exact same keywords. for example the string received can be "This is a message in string format received from websockets" so the above condition will fail as the whole string does not match.

    I have tried this too:

    var post = "This is a string received from websockets"
    textbox.text = post;
    var strfind = textbox.text.indexOf("This")
    console.log(strfind)
    var strfind2 = textbox.text.indexOf("String")
    if (textbox.text.indexOf("This") === strfind) {
    
            somebutton.visisble=true;
    }
    

    strfind returns 0 which is true but the above if statement still fails.


  • Moderators

    @AexAcad

    textbox.text is not an array, and the second condition fails even if the conditions are the same,

    So if (textbox.text[1] === "This" && textbox.text[3] === "string") { wont work.

    indexOf will return a valid index if the string if found and -1 otherwise so why not check for this condition ?



  • @p3c0

    I got it working thanks!! I used the following logic:

    var post = "This is a string received from websockets"
    textbox.text = post;
    var strfind = textbox.text.indexOf("This")
    console.log(strfind)
    if (textbox.text.indexOf("This") === 0) {
            somebutton.visisble=true;
    }
    else {
        somebutton.visible=false;
    }
    

  • Moderators

    @AexAcad
    Great! But beware you are always checking against a fixed index i.e 0. Maybe you can make use of -1 which is returned when string is not found ?



  • @p3c0

    I could use this:

    if (textbox.text.indexOf("Register") != -1 && texbox.text.indexOf("Device") != -1) {
            somebutton.visisble=true;
    }
    

    But the websocket application I am trying to communicate with is more like a client chat application where if the input string received from websockets contains ("Register") and ("Device") the button would always become visible to the user instead of when I would want only the button to be visible on a string that has a fixed format for example "Register your Device at http://example.com with paring code: xyz"


  • Moderators

    @AexAcad Ok.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.