簡體   English   中英

用Java中的replaceAll格式化字符串問題

[英]Formatting string problems with replaceAll in Java

我有一個字符串:

String strung = (1.0√(10.0))/1.0

撥打以下電話后:

strung = strung.replaceAll(".0","");
strung = strung.replaceAll("1√","√");

我不希望得到這個:

(√())/1

我只需要獲取以下內容,但很難進行設置

√10

感謝您的任何建議

使用replace ,而不是replaceAll

replaceAll的參數分別解釋為正則表達式和正則表達式替換。 因此, . 被解釋為“任何字符”。

replace從字面上接受參數,如"."中所示"." 僅匹配字符串中的句點。

當然,您也可以繼續使用replaceAll ,但可以轉義.

strung = strung.replaceAll("\\.0","");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM