让我们说我有一个6个字符的输入字符串,后跟一个单词"你好"
abcdefhello
或另一组数据
quernwegngweghello
我的意思是,"你好"肯定会在最后。如果可能,我怎样才能删除这个词?
字符串在数量上是动态的,这意味着它会相应地改变,所以我想检查这个单词是否包含"你好"在最后5个字符中,如果可能,将其删除。
答案 0 :(得分:1)
private static String stripHelloFromEnd(String input) {
if (input.endsWith("hello")) {
return input.substring(0, input.length() - "hello".length());
}
return input;
}