1,判断最后的字符是不是possible, 用endswith方法,接下来再替换
2,替换最后一个可以变项的作
String s = "The possible problem is, How is that possible";
if(s.endsWith("possible")){
String s1 = s.substring(0, s.lastIndexOf("possible"));
s = s1 + "impossible";
}
System.out.println(s);
2,替换最后一个可以变项的作
String s = "The possible problem is, How is that possible";
if(s.endsWith("possible")){
String s1 = s.substring(0, s.lastIndexOf("possible"));
s = s1 + "impossible";
}
System.out.println(s);
字符串操作案例
本文介绍了一个简单的字符串操作案例,演示了如何使用endswith方法检查字符串是否以特定子串结尾,并通过substring和lastIndexOf方法进行替换操作。
404

被折叠的 条评论
为什么被折叠?



