如何在Java中修剪字符串“ 120 ### 16 ### 16 ###”,仅切掉没有空格的数字,以将其置于3个单独的文本视图中?字符串从arduino通过距离传感器的蓝牙发出。
您可以使用正则表达式获取所有不同的数字:
Pattern p = Pattern.compile("(\\d+)");
Matcher matcher = p.matcher("120 ### 16 ### 16 ###");
while (matcher.find()) {
System.out.println(matcher.group());
}
此输出:
120
16
16
您可以检查它here。
检查下面的代码是否符合您的目的。
public class Main
{
public static void main(String[] args) {
String s = "120 ### 16 ### 16 ###";
// removing # from the string
s = s.replace("#", "");
//splitting the string with white spaces in an array
String[] arr = s.split(" ");
//printing an array
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}