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);