3

I've been using the .indexOf('') > -1 in order to check whether there's a match in a string. The problem that I'm having is that when I'm performing the match on multiple strings, I get a match on the string for both EIFT and EI (since EIFT contains EI), and so the function returns true for both sentences. What I need is a way for this to only return true for function eIft if the string is "EIFT", but not for EI.

My current code is as follows, and I've been trying to think of ways around this but haven't had any success yet.

function eI(mystring){
    return mystring.indexOf("EI") > -1
}

function eIft(mystring){
    return mystring.indexOf("EIFT") > -1
}

Thanks!

5
  • Can you clarify what is wrong with the function eIft? It will return false if myString contains EI but not EIFT. Commented Sep 10, 2014 at 21:26
  • I don't understand. If it contains EIFT then it also contains EI, so why shouldn't it match? Are you saying that you want to prefer the longer over the shorter? If so, then search for the longer first. Commented Sep 10, 2014 at 21:27
  • Is there a reason you don't respond to requests for clarification? Commented Sep 10, 2014 at 21:44
  • @cookiemonster @cybersam, the functions work, but the problem lies in that the function eI will return true for string "blah blah eift", when I only want function eIft to return true for "blah blah eift". Hope that clears up your questions :) Commented Sep 11, 2014 at 13:39
  • @maudulus: No, not really. It's an unreasonable expectation for the eI function to not return true when it's designed to return true. And it's even less clear now that the answer you accepted below would return false for both functions. Your problem's description is vague. What is the XY Problem Commented Sep 11, 2014 at 18:13

2 Answers 2

2

You can use ===; that will do an exact match of strings. Use indexOf only if you're checking whether the string contains another string.

function eI (mystring) {
    return mystring === "EI";
}

function eIFt(mystring) {
    return mystring === "EIFT";
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are checking inside a string for you values (e.g. heleilo), then you need to confirm your positive results for the 'EI' check:

function eI(mystrng) {
    return mystring.indexOf("EI") != -1 && !eIFt(mystring);
}

This would only work provided they don't both exist in different occurences (e.g. heleileifto). In this case, you have to check the immediate following characters:

function eI(mystring) {
    var pos = mystring.indexOf("EI");

    if (pos != -1) { // found
        var char1 = mystring[pos + 2];
        var char2 = mystring[pos + 3];

        return char1 !== 'F' && char2 !== 'T';
    }
}

OR

function eI(mystring) {
    var pos = mystring.indexOf("EI");

    if (pos != -1) { // found
        return pos != eIFt(mystring); // they won't have the same index
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.