Monday, May 13, 2024
62
rated 0 times [  69] [ 7]  / answers: 1 / hits: 5950  / 10 Years ago, wed, september 10, 2014, 12:00:00

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!


More From » string-matching

 Answers
24

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

[#42580] Tuesday, September 9, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;