String Multiple methods for intercepting strings

1. Intercept the string and return the string array:
  String str="123,abc,456,def";
  String[]  strs=str.split(",");
  for (int i=0;i<strs.length;i++){
    
    
     Log.e("TAG","打印第"+i+"个数据是---"+strs[i]);
  }      
  • The printed results are as follows:
打印第0个数据是---123
打印第1个数据是---abc
打印第2个数据是---456
打印第3个数据是---def
Second, specify the index subscript:
  • 2 start index 5 end index
 String str="0123456789";
 //指定索引 2开始索引 5结束索引
 String substring = str.substring(2, 5);
 Log.e("TAG","打印数据是---"+substring);
  • The printed results are as follows:
打印数据是---234
3. Specify the index subscript:
  • index 4 to end
 String str="0123456789";
 //指定索引 4到末尾
 String substring = str.substring(4);
 Log.e("TAG","打印数据是---"+substring);
  • The printed results are as follows:
打印数据是---456789
4. Specify the index to the specified character "?":
  • indexOf 0 starts to the first character "_"
  • indexOf 0 starts to the last character "_"
 String str="123_abc_456";
 String substring = str.substring(0,str.indexOf("_"));
 String substring1 = str.substring(0,str.lastIndexOf("_"));
 String substring2 = str.substring(str.lastIndexOf("_")+1);
 Log.e("TAG","打印数据是---"+substring);
 Log.e("TAG","打印数据是---"+substring1);
 Log.e("TAG","打印数据是---"+substring2);
  • The printed results are as follows:
打印数据是---123
打印数据是---123_abc
打印数据是---456
5. Intercept specified digits from the back:
 String message="123456789_00";
 String substring = message.substring(message.length()-5);
 Log.e("TAG","打印数据是---"+substring);
  • The printed results are as follows:
打印数据是---89_00

Guess you like

Origin blog.csdn.net/afufufufu/article/details/131081075