replaceAll Java方法从String中删除“\\ n”

时间:2018-03-29 22:02:00

标签: java regex string

我有一个简单的治疗,但我被困住了

我有类似

的东西
"\"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);

    }

结果是..我总是“\n”我想删除它

iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzt3XecXVW99

3 个答案:

答案 0 :(得分:3)

也许:

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

需要:

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

(抱歉,我不能只是添加评论)

答案 1 :(得分:1)

你可以试试这个:

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

}

答案 2 :(得分:1)

你基本上有两个选择,两个都在下面的代码中:

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