Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串中删除美元和逗号_Java_Regex_Str Replace - Fatal编程技术网

Java 从字符串中删除美元和逗号

Java 从字符串中删除美元和逗号,java,regex,str-replace,Java,Regex,Str Replace,如何从同一字符串中删除美元符号($)和所有逗号(,)?避免使用正则表达式会更好吗 String liveprice = "$123,456.78"; 详情如下: 这样行吗 String liveprice = "$123,456.78"; String newStr = liveprice.replace("$", "").replace(",",""); 输出:123456.78 更好的一个: String liveprice = "$123,456.78"; String newStr

如何从同一字符串中删除美元符号($)和所有逗号(,)?避免使用正则表达式会更好吗

String liveprice = "$123,456.78";
详情如下:

这样行吗

String liveprice = "$123,456.78";
String newStr = liveprice.replace("$", "").replace(",","");
输出:
123456.78

更好的一个:

String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "")
试试看

replaceAll
使用正则表达式,避免使用正则表达式,而不是使用连续的
replace
方法

 String liveprice = "$1,23,456.78";
 String newStr = liveprice.replace("$", "").replace(",", "");

只需使用
替换

String liveprice = "$123,456.78";
String output = liveprice.replace("$", "");
output = output .replace(",", "");
你喜欢这样吗

NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());
输出

123456.78

你真的需要更换吗

public void test() {
  String s = "$123,456.78";
  StringBuilder t = new StringBuilder();
  for ( int i = 0; i < s.length(); i++ ) {
    char ch = s.charAt(i);
    if ( Character.isDigit(ch)) {
      t.append(ch);
    }
  }
}
公共无效测试(){
字符串s=“$123456.78”;
StringBuilder t=新的StringBuilder();
对于(int i=0;i

这适用于任何修饰数字。

如果没有正则表达式,您可以尝试以下方法:

String output = "$123,456.78".replace("$", "").replace(",", "");

使用瑞典克朗货币的示例

字符串x=“19.823.567,10 kr”

系统输出println(x)


将给出输出->19823567.10(现在可以用于任何计算)

我认为您可以使用regex。例如:

"19.823.567,10 kr".replace(/\D/g, '')

就我而言,@Prabhakaran的回答不起作用,有人可以试试这个

String salary = employee.getEmpSalary().replaceAll("[^\\d.]", "");
Float empSalary = Float.parseFloat(salary);

如果不需要货币,则不需要
getCurrencyInstance()

@Jesper现在可以了吗?他想避免使用正则表达式。Jesper说,“如果我们避免使用正则表达式,效果会更好。”这将在数字之间设置间隙
。我真的想要这个。你让我开心。谢谢:)我的值是1000然后是gives java.text.ParseException:不可解析的数字:“1000”(在偏移量5处)“$”是本地货币的货币字符吗?
        x=x.replace(".","");
        x=x.replaceAll("\\s+","");
        x=x.replace(",", ".");
        x=x.replaceAll("[^0-9 , .]", "");
"19.823.567,10 kr".replace(/\D/g, '')
String salary = employee.getEmpSalary().replaceAll("[^\\d.]", "");
Float empSalary = Float.parseFloat(salary);
import java.text.NumberFormat

def currencyAmount = 9876543.21 //Default is BigDecimal
def currencyFormatter = NumberFormat.getInstance( Locale.US )

assert currencyFormatter.format( currencyAmount ) == "9,876,543.21"