我正在打印一个数组,但我只想显示数字。我想从 String
中删除括号和逗号(只保留数字)。到目前为止,我已经能够删除逗号,但我正在寻找一种向 replaceAll
方法添加更多参数的方法。
如何删除括号和逗号?
cubeToString = Arrays.deepToString(cube);
System.out.println(cubeToString);
String cleanLine = "";
cleanLine = cubeToString.replaceAll(", ", ""); //I want to put all braces in this statement too
System.out.println(cleanLine);
输出是:
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5]]
[[0000][1111][2222][3333][4444][5555]]
最佳答案
可以使用特殊字符[
和]
组成一个模式,然后用\\
转义[
和 ]
(来自您的输入),例如,
cleanLine = cubeToString.replaceAll("[\\[\\]\\s,]", "");
或替换不是数字的所有内容。喜欢,
cleanLine = cubeToString.replaceAll("\\D", "");
关于java - 替换字符串中的多个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477155/