![](/img/trans.png)
[英]NumberFormatException on StringUtils.isBlank method
[英]StringUtils Overload chop method
我想重載StringUtils
的chop方法以刪除最后一個字符(如果它是反斜杠(\\)
。
是否可以重載該函數,或者我需要使用if條件?
為什么不這樣做呢?
StringUtils.removeEnd(s,"\\")
是的,使用if
語句。 您不能覆蓋靜態方法,為此僅創建它自己的類將是太多了。
我個人喜歡其他選擇:
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));
}
這樣的事情。 然后,您可以將其用於任何字符。 根據需要進行調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.