简体   繁体   English

在Android应用程序中分解字符串

[英]Decomposing strings in Android Application

I am trying to write an Android Application in Java for an Android phone. 我正在尝试用Java为Android手机编写一个Android应用程序。 The Android application reads serial data strings via Bluetooth using SPP profile. Android应用程序使用SPP配置文件通过蓝牙读取串行数据字符串。 These strings are sent by another Bluetooth device. 这些字符串由另一个蓝牙设备发送。 These strings are in the following format: 这些字符串的格式如下:

"Acc Data:x_adc,y_adc,z_adc!"

Each string begins with: 每个字符串都以:

"Acc Data: “访问控制数据:

and ends with 并以

"!" “!”

The x_adc, y_adc and z_adc are values and have a length of 5 digits. x_adc,y_adc和z_adc是值,长度为5位数字。

These strings are sent at regular intervals. 这些字符串是定期发送的。 Right now I am able to use InputStream to accept these strings using a string buffer. 现在,我可以使用InputStream使用字符串缓冲区来接受这些字符串。 I'm also able to display the complete strings in a scrollable TextView which displays each string beneath the other. 我还能够在可滚动的TextView中显示完整的字符串,该文本视图将每个字符串显示在另一个字符串之下。

What I want to do is to decompose the x_adc, y_adc and z_adc values from the strings and display these values in a 3 separate TextViews. 我要做的是从字符串中分解x_adc,y_adc和z_adc值,并在3个单独的TextViews中显示这些值。 The TextViews which need to display the adc values have to refresh after a new string has been sent to the Android smartphone. 在将新字符串发送到Android智能手机后,需要刷新显示adc值的TextView。 I tried to use the Pattern class and split() to decompose the strings but failed to understand them and use them in the application. 我试图使用Pattern类和split()分解字符串,但无法理解它们并在应用程序中使用它们。

Could someone help me with this problem? 有人可以帮我解决这个问题吗?

You could use substring() to clean the extra data, and then split() : 您可以使用substring()清理多余的数据,然后使用split()

String[] values = indata.substring(9, 26).split(",");
String x_adc = values[0];
...

Try: 尝试:

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    update("Acc Data:x_adc,y_adc,z_adc!");

    public void update(String data) {
        String values[] = data.replace("Acc Data:", "").replace("!", "").split(",");
        String v1 = "";
        String v2 = "";
        String v3 = "";
        if(values != null && values.length == 3) {
            v1 = values[0];
            v2 = values[1];
            v3 = values[2];
        }
        tv1.setText(v1);
        tv2.setText(v2);
        tv3.setText(v3);
    }

Alternate way of getting the values: 获取值的替代方法:

String values[] = data.substring(data.indexOf(':') + 1, data.indexOf('!')).split(",");

只需在String函数substring(beginIndex, endIndex)上放一下即可抑制字符串的静态部分,然后使用split(",")获得3个值。

This code will handles instances where > 3 tokens are included in the String . 此代码将处理在String中包含> 3个标记的实例。

public static void main(String[] args) {
    String value = "Acc Data:x_adc,y_adc,z_adc!";

    String trimmed = value.substring(value.indexOf(":")+1, value.length()-1);
    String[] tokens = trimmed.split(",");

    for(String token: tokens){
        System.out.println(token);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM