我有一个字符串
String str = "Hello there how are you"
和子字符串
String substr = "how are you".
我正在检查字符串是否包含这样的子字符串:
if (str.toLowerCase().contains(substr.toLowerCase())) {
// now check if substr is the last part of str
}
如果字符串中的子字符串是,我想检查子字符串是否是字符串的最后一部分。无论如何我能做到吗?谢谢!
答案 0 :(得分:3)
你会想要String.endsWith()
方法:
if (str.toLowerCase().endsWith(substr.toLowerCase())
答案 1 :(得分:1)
如果你想知道的话,你可以使用endsWith
方法而不是多步法:
if (str.toLowerCase().endsWith(substr.toLowerCase())) {
如果你需要知道两者(因为你的内容超出下面的第二个if
),和/或做更高级的其他事情,你可以使用单独的行:
if (str.toLowerCase().contains(substr.toLowerCase())) {
if (str.toLowerCase().endsWith(substr.toLowerCase())) {
}
}