문제

I made this so only messages starting with '/msg' or '/logout' will submit.

But, users can still send messages! Is something wrong with my code?

if ((msg.indexOf("/msg") != 0) && (msg.indexOf("/logout") != 0))
{
    return;
}
도움이 되었습니까?

해결책

indexOf will return -1 if the String is not found otherwise it will return the index found which is >= 0

So your test have to be:

if ((msg.indexOf("/msg") < 0) && (msg.indexOf("/logout") < 0))
{
 return;
}

or

if ((msg.indexOf("/msg") == -1) && (msg.indexOf("/logout") == -1))
{
 return;
}

다른 팁

Turns out my code did work. The issue was in code I neglected to show you but I fixed it.

Thanks for the help anyway. :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top