![](/img/trans.png)
[英]How to move a null to the end of array in java using FOR loop?
[英]How to delete , Null(""), from the end using Java
正如問題的標題所述:如何從字符串中刪除, Null(""),
?
我已經嘗試了以下代碼,但它不起作用:
String c = "customer_date, privacy_code, Null(""), ";
String nd = "Null(\"\")";
c = c.substring(0, c.lastIndexOf(nd));
如果只想從字符串的末尾刪除它,可以使用String#replaceAll
:
nd = nd.replaceAll("Null\\(\"\"\\),$", "");
由於它接受正則表達式,因此我添加了與字符串末尾匹配的$
特殊字符。
請訪問String API以發現許多對您有幫助的有用方法。
如果您不使用轉義字符,您的代碼將無法編譯,無論如何您都不必用引號麻煩自己,您只能使用 String nd = "Null"; 而是像這樣
String c="customer_date, privacy_code, Null(\"\"), ";
String nd = "Null";
c=c.substring(0,c.lastIndexOf(nd));
System.out.println(c);
因為無論如何你要刪除的一段字符串都是以 NULL 開頭的
我喜歡用大錘敲碎堅果!
String in = "customer_date, privacy_code, Null(\"\"), ";
String out = Arrays.asList(in.split(",")).stream()
.map(String::trim)
.filter(s -> !s.equals("Null(\"\")"))
.collect(Collectors.joining(", "));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.