replaceAll Java method to remove “\\n” from String [duplicate]

梦想与她 提交于 2021-02-17 07:14:41

问题


I have a simple treatement but I'm stuck

I have something like

"\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";

public static void main(String[] args) {
        String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
        String filtre1 = value.replaceAll("\"", "");
        String filtre2 = filtre1.replaceAll("\\n", "");
        System.out.println(filtre2);

    }

the result is .. I have always "\n" I want to remove it

iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzt3XecXVW99

回答1:


Maybe:

String filtre2 = filtre1.replaceAll("\\n", ""); 

need to be :

String filtre2 = filtre1.replaceAll("\\\\n", ""); 

(sorry i cannot just add comment)




回答2:


You could try this:

public static void main(String[] args) {
    String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
    String filtre1 = value.replaceAll("\"", "");
    String filtre2 = filtre1.replaceAll("\\\\n", "");
    System.out.println(filtre2);

}



回答3:


You basically have two choices, both of which are in the below code:

public static void main(String[] args) {
    String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
    System.out.println(value.replace("\"", "").replace("\\n", ""));
    System.out.println(value.replaceAll("\"|\\\\n", ""));
}


来源:https://stackoverflow.com/questions/49565658/replaceall-java-method-to-remove-n-from-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!