Change
String text=sb.toString();
to
String text=sb.toString().replace('[', ' ').replace(']', ' ');
Вопрос
How to remove [ ] from my output?
String omega_str[][] ;
StringBuilder sb=new StringBuilder();
for (String[] row:omega_str)
sb.append(Arrays.toString(row));
String text=sb.toString();
System.out.println("Text : "+text);
My output is:
[-0.06409677515180673][0.12516724483856012][-0.06188844693840026][0.1542289929525214][-0.06603595481351604][0.07823062287735344][0.10161817634744892]
expected output
-0.06409677515180673 0.12516724483856012 -0.06188844693840026 0.1542289929525214 -0.06603595481351604 0.07823062287735344 0.10161817634744892
How to do this?
Решение 3
Change
String text=sb.toString();
to
String text=sb.toString().replace('[', ' ').replace(']', ' ');
Другие советы
You should not first produce something, then remove it. The [...]
is the result of Arrays.toString
. You better loop through the subarrays and print the values to the string builder.
BTW, it looks like you have only ever a single value in the subarray. (This raises the question, if you really need a 2-dimensional array.) Anyway, if you know that there is only a single value, you do not even need a loop, just
sb.append(' ');
sb.append(row[0]);
Use regex with replaceAll
String str="[-0.06409677515180673][0.12516724483856012]"
+ "[-0.06188844693840026][0.1542289929525214]"
+ "[-0.06603595481351604][0.07823062287735344]"
+ "[0.10161817634744892]";
str=str.replaceAll("[\\[|\\]]", " ");
System.out.println(str);
-0.06409677515180673 0.12516724483856012 -0.06188844693840026 0.1542289929525214 -0.06603595481351604 0.07823062287735344 0.10161817634744892
Simply use a substring()
:
yourString.substring(1, yourString.length() - 1);
Skip [ ]
before add it into StringBuilder
String str= Arrays.toString(row);
str= str.substring(1,str.length()-1);
sb.append(str);
Calling .toString() to an array will always generate such output, so I'd suggest you to replace "[]"with an empty string "" after calling .toString() on an array like this:
line.replace("[", "").replace("]", "");
but it's a good solution only for short strings due to performance issuses.
try replaceAll
with escape characters:
String str = "[-0.06409677515180673][0.12516724483856012][-0.06188844693840026][0.1542289929525214][-0.06603595481351604][0.07823062287735344][0.10161817634744892]";
str = str.replaceAll("\\[", "").replaceAll("\\]","");
this will do the trick for you.
for (String[] row:omega_str) {
for(String val : row) {
sb.append(val + " ");
}
}
String text=sb.toString();
System.out.println("Text : "+text);
as @Ingo suggested don't produce something which you want to delete
I believe that using regex
in order to remove what I don't like from the toString
method is not a good approach to the problem.
I will simply create a method in your class with a good name (i.e. print....) like this:
public static void printArray(String[][] array) {
System.out.print("Text :");
for (String[] row : array)
for (String element : row)
System.out.print(" " + element);
}
And simply call:
printArray(omega_str);
Using this solution you don't have to use either the StringBuilder
instance.
Ciao!