如何在Java中以正确的方式修剪字符串并将其拆分为3个单独的textviews

问题描述 投票:-1回答:2

如何在Java中修剪字符串“ 120 ### 16 ### 16 ###”,仅切掉没有空格的数字,以将其置于3个单独的文本视图中?字符串从arduino通过距离传感器的蓝牙发出。

java android string trim
2个回答
0
投票

您可以使用正则表达式获取所有不同的数字:

Pattern p = Pattern.compile("(\\d+)");
Matcher matcher = p.matcher("120 ### 16 ### 16 ###");
while (matcher.find()) {
    System.out.println(matcher.group());
}

此输出:

120
16
16

您可以检查它here


0
投票

检查下面的代码是否符合您的目的。

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]);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.