簡體   English   中英

在Java中使用String的方法replaceall

[英]Using method replaceall of String in Java

我只想從字符串中獲取字符: +,-,/,x
但這說-是多余的字符范圍。

String operacoes=texto.replaceAll("[^+?-?/?x]+"," ");

我該如何工作?

只需添加\\\\之前-字符。 因為-字符在正則表達式中以各種方式使用,例如[AZ] [0- 9]等。因此要確定-您需要在Java中放置\\\\ ,因為Java中的字符串不支持單個\\

    String texto="3 + 4 - 5 + 4";
    String operacoes=texto.replaceAll("[^+?\\-?/?x]+"," ");
    System.out.println(""+operacoes);

您必須使用雙反斜杠轉義減號。

String operacoes=texto.replaceAll("[^+?\\-?/?x]+"," ");

由於字符-用於正則表達式

String texto = "1+2-3/4x5";
String operacoes=texto.replaceAll("[^+?\\-?/?x]+"," ");
System.out.println(operacoes);

產生輸出

+ - / x 

暫無
暫無

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

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