I want to overload the chop method of StringUtils
to remove the last character if it is a backslash (\\)
.
Is it possible to overload the function or I need to use an if condition?
为什么不这样做呢?
StringUtils.removeEnd(s,"\\")
yes, use an if
statement. You can't override a static method and it would be too much anyway to create it's own class just for that.
I have an alternative that I personally like:
public static String chopIf(Predicate<String> p, String s) {
if (p.test(s)) {
return s.substring(0, s.length()-1); //or StringUtils.chop(s)
}
return s;
}
public static void main(String[] args) {
String test = "qwert\\";
System.out.println(chopIf(s -> s.endsWith("\\"), test));
}
Something like that. Then you can use it for any character. Tweak it according to need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.