我有代码将一个字符串拆分为 3 个字符串,然后打印它们。我希望每个人都被相当于“Enter”的人分开。这是代码:
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x = tempArr[0] + "_"; //I want the enter to be where the underlines are:
String y = tempArr[1] + "_";
String z = tempArr[2] + "_";
for (String a : tempArr) {
writer.println("<h3 style=padding-left:20px;>" + x + y
+ z + "</h3><br>");
}
writer.flush();
writer.close();
} catch (IOException e) {
// put notification here later!!!
e.printStackTrace();
}
这个输出:
x=-0.125_y=0.9375_z=0.375
x=-0.125_y=0.9375_z=0.375
字符串之间用下划线分隔。
但是,我希望它看起来像这样:
x=-0.125
y=0.9375
z=0.375
x=-0.125
y=0.9375
z=0.375
感谢您的帮助。
编辑:
我已经在下面的代码中实现了@Julius 的答案,打印出我想要的方式:
代码:
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x = tempArr[0];
String y = tempArr[1];
String z = tempArr[2];
for (String a : tempArr) {
writer.println("<h3 style=padding-left:20px;>" + x + "<br>" + y + <br>
+ z + "</h3><br>");
}
writer.flush();
writer.close();
} catch (IOException e) {
// put notification here later!!!
e.printStackTrace();
}
打印:
x=0.25
y=125
z=1.23
x=0.125
y=725
z=0.935
最佳答案
如果你想在浏览器中显示返回行,这是要走的路:
writer.println("<h3 style=padding-left:20px;>" + x + "<br/>" + y + "<br/>" + z + "<br/></h3>");
关于java - 用 "Enter"分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251510/