当前位置:   article > 正文

java 字符串数据_java——字符串中数据提取

message.substring( )

public String substring(int beginIndex, int endIndex);

返回一个新字符串,它是这个字符串的一个子字符串,这个子字符串从指定的beginIndex开始,直到索引到exdIndex-1处的字符,内容包含形式  [beginIndex,endIndex);

public class Demo

{

public static void main(String[] args)

{

String aString ="123456789";

String r1 =aString.substring(aString.length()-3, aString.length()-1);

System.out.println(r1);

}

}

打印结果是78

android备用案例:

public final Handler myHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case MESSAGE_TOAST:

Toast.makeText(getApplicationContext(),

msg.getData().getString(TOAST), Toast.LENGTH_SHORT)

.show();

break;

case MESSAGE_READ:

byte[] readBuf = (byte[]) msg.obj;

try {

//横向的textview 对应的ID 2 101 102 99 98 97

String readMessage = new String(readBuf, 0, msg.arg1);

//温度前二个字符

String rr =readMessage.substring(readMessage.length()-9, readMessage.length()-7);

//温度第三个字符

String di =readMessage.substring(readMessage.length()-7, readMessage.length()-6);

//浊度二个字符

String zhuodu =readMessage.substring(readMessage.length()-6, readMessage.length()-4);

//方向一个字符

String fangxiang =readMessage.substring(readMessage.length()-4, readMessage.length()-3);

//液位 二个字符

String yewei =readMessage.substring(readMessage.length()-3, readMessage.length()-1);

TextView textview1 = (TextView) findViewById(R.id.textview2);

textview1.setText(rr);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/617971
推荐阅读
相关标签
  

闽ICP备14008679号