我正在尝试使用Java中的replaceall函数删除所有破折号(-)和逗号(,)。但是,我只能删除破折号或逗号。我怎样才能解决这个问题?

if (numOfViewsM.find()){
    if (numOfViewsM.toString().contains(","))
    {
        numOfViews =Integer.parseInt(numOfViewsM.group(1).toString().replaceAll(",", ""));
    }
    else if (numOfViewsM.toString().contains("-"))
    {
        numOfViews = Integer.parseInt(numOfViewsM.group(1).toString().replaceAll("-", ""));
    }
    else
        numOfViews = Integer.parseInt(numOfViewsM.group(1));
}

最佳答案

replaceall(regex, String)方法使用正则表达式。您可以使用一条语句来做到这一点

String output= str.replaceAll("[-,]", "");

08-16 16:00