索引不工作?

发布于 2025-01-03 04:18:40 字数 187 浏览 3 评论 0原文

我这样做是为了只提交以“/msg”或“/logout”开头的消息。

但是,用户仍然可以发送消息!我的代码有问题吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉徒眸意 2025-01-10 04:18:40

indexOf<如果找不到 String , /a> 将返回 -1,否则它将返回找到的索引,即 >= 0

所以你的测试必须是:

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

if ((msg.indexOf("/msg") == -1) && (msg.indexOf("/logout") == -1))
{
 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;
}
嘦怹 2025-01-10 04:18:40

事实证明我的代码确实有效。问题出在代码中,我忽略了向您展示,但我修复了它。

不管怎样,谢谢你的帮助。 :)

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文