Radin Hakimjavadi Radin Hakimjavadi - 1 year ago 128
Java Question

How do I check if a string contains a word without case sensitivity?

I looked at the other questions like this but I couldn't find one that worked. I'm trying to check if the person replied with something meaning "yes", so I'm checking for several words that mean yes. But if they reply with "yEs", my code won't work because its case sensitive, and I don't know what to do.


String fq = JOptionPane.showInputDialog(null, "Welcome to RadinBot! Would you like to continue?", "RadinBot", JOptionPane.YES_NO_CANCEL_OPTION);

if (fq.contains("yes") ||
fq.contains("Yeah") ||
fq.contains("Yup") ||
fq.contains("mhm") ||
fq.contains("Yea") ||

Answer Source

You can just use toLowerCase() to remove the case sensitivity:

if (fq.toLowerCase().contains("yes") || fq.toLowerCase().contains("yeah") || fq.toLowerCase().contains("yup") || fq.toLowerCase().contains("mhm") || fq.toLowerCase().contains("yea") || fq.toLowerCase().contains("sure"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download