如何在string中使用contains和equalsIgnoreCase [英] How to use contains and equalsIgnoreCase in string
本文介绍了如何在string中使用contains和equalsIgnoreCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法检查一个字符串是否包含某些内容而不区分大小写?
Is there a way to check if a string contains something while not being case sensitive?
例如:(此代码无效,只是为了让你得到一个基本了解我的问题)
For example: (this code is invalid it's just for you to get a basic understanding of my question)
String text = "I love ponies";
if(text.contains().equalsIgnoreCase("love") {
// do something
}
编辑:
--------仍然无法正常工作
-------- Still not working
哦,转它不起作用。这是我正在使用的。
(这是游戏的诅咒过滤器)
ooh, turns out it's not working. Here's what I'm using. (it's a curse filter for a game)
public void onChat(PlayerChatEvent event) {
Player player = event.getPlayer();
if (event.getMessage().contains("douche".toLowerCase()) || /* More words ... */) {
event.setCancelled(true);
player.sendMessage(ChatColor.GOLD + "[Midnight Blue] " + ChatColor.RED + "Please Don't Swear.");
}
}
它适用于小写而非大写。
It works with lowercase but not uppercase.
推荐答案
return text.toLowerCase().contains(s2.toLowerCase());
或者另一种方式是
Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(text).find();
这篇关于如何在string中使用contains和equalsIgnoreCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文