Java 这个replaceAll()有什么问题?
我得到的输出是一次打印Java 这个replaceAll()有什么问题?,java,string,replaceall,Java,String,Replaceall,我得到的输出是一次打印x的值,其余两个println打印空行 1.234.567,89 Process finished with exit code 0 我做错了什么 public class Dummy { public static void main(String args[]) { String x = "1.234.567,89 EUR"; String e = " EUR"; x = x.replaceAll(" E
x
的值,其余两个println
打印空行
1.234.567,89
Process finished with exit code 0
我做错了什么
public class Dummy {
public static void main(String args[]) {
String x = "1.234.567,89 EUR";
String e = " EUR";
x = x.replaceAll(" EUR","");
System.out.println(x);
x = x.replaceAll(".", "");
System.out.println(x);
x = x.replaceAll(",",".");
System.out.println(x);
//System.out.println(x.replaceAll(" EUR","").replaceAll(".","").replaceAll(",","."));
}
}
问题是
x=x.replaceAll(“.”,”)
将每个字符替换为”
,因此在第二个replaceAll()
之后有一个空的x
请注意,replaceAll()
方法的第一个参数是正则表达式
将其更改为:
x = x.replaceAll("\\.", "");
问题是
x=x.replaceAll(“.”,”)
将每个字符替换为”
,因此在第二个replaceAll()
之后有一个空的x
请注意,replaceAll()
方法的第一个参数是正则表达式
将其更改为:
x = x.replaceAll("\\.", "");
方法将正则表达式作为第一个参数。正则表达式中的
匹配除换行符以外的任何字符。这就是为什么它正在取代一切
你可以用它来代替
方法将正则表达式作为第一个参数。正则表达式中的
匹配除换行符以外的任何字符。这就是为什么它正在取代一切
你可以用它来代替
使用:
告诉Java,
不是具有特殊含义的正则表达式,而是字符串
其他解决办法:
- 使用接受字符串的
replace
- 通过
对\\\.
进行转义(转义正则表达式由
完成,但在Java中\
是由\
编写的)\
不是具有特殊含义的正则表达式,而是字符串
其他解决办法:
- 使用接受字符串的
replace
- 通过
对\\\.
进行转义(转义正则表达式由
完成,但在Java中\
是由\
编写的)\
- 此字符串要与之匹配的正则表达式
dot
使用(\
),Java需要双反斜杠(\\
)
转义点后的固定代码如下所示
public static void main(String args[]) {
String x = "1.234.567,89 EUR";
String e = " EUR";
x = x.replaceAll(" EUR","");
System.out.println(x);
x = x.replaceAll("\\.", "");
System.out.println(x);
x = x.replaceAll(",",".");
System.out.println(x);
}
从中读取JavaDoc
regex
- 此字符串要与之匹配的正则表达式
dot
使用(\
),Java需要双反斜杠(\\
)
转义点后的固定代码如下所示
public static void main(String args[]) {
String x = "1.234.567,89 EUR";
String e = " EUR";
x = x.replaceAll(" EUR","");
System.out.println(x);
x = x.replaceAll("\\.", "");
System.out.println(x);
x = x.replaceAll(",",".");
System.out.println(x);
}
使用
而不是
System.out.println(x.replaceAll(" EUR","").replaceAll(".","")
.replaceAll(",","."));
你必须用\\\\.
您可以在一行中执行此操作,如下所示
System.out.println(x.replaceAll(" EUR|\\.|,",""));
使用
而不是
System.out.println(x.replaceAll(" EUR","").replaceAll(".","")
.replaceAll(",","."));
你必须用\\\\.
您可以在一行中执行此操作,如下所示
System.out.println(x.replaceAll(" EUR|\\.|,",""));
作为替代解决方案: 考虑使用或。NumberFormat提供了一种方法 例如,尝试:
final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.GERMANY);
if (currencyFormat instanceof DecimalFormat) {
final DecimalFormat currencyDecimalFormat = (DecimalFormat) currencyFormat;
final DecimalFormatSymbols decimalFormatSymbols = currencyDecimalFormat.getDecimalFormatSymbols();
decimalFormatSymbols.setCurrencySymbol("EUR");
currencyDecimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
currencyDecimalFormat.setParseBigDecimal(true);
System.out.println(currencyFormat.format(new BigDecimal("1234567.89")));
final BigDecimal number = (BigDecimal) currencyFormat.parse("1.234.567,89 EUR");
System.out.println(number);
}
作为替代解决方案: 考虑使用或。NumberFormat提供了一种方法 例如,尝试:
final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.GERMANY);
if (currencyFormat instanceof DecimalFormat) {
final DecimalFormat currencyDecimalFormat = (DecimalFormat) currencyFormat;
final DecimalFormatSymbols decimalFormatSymbols = currencyDecimalFormat.getDecimalFormatSymbols();
decimalFormatSymbols.setCurrencySymbol("EUR");
currencyDecimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
currencyDecimalFormat.setParseBigDecimal(true);
System.out.println(currencyFormat.format(new BigDecimal("1234567.89")));
final BigDecimal number = (BigDecimal) currencyFormat.parse("1.234.567,89 EUR");
System.out.println(number);
}
使用
x=x.replaceAll(“[.]”,“”)
第一个参数是正则表达式。您到底想做什么?使用x=x.replaceAll(“[.]”,“”)第一个参数是正则表达式。你到底想做什么?