如有一个字符串str为access_token=7BBFBDAE62CA6B9EC0F4B4E810F1C38C&expires_in=7776000&refresh_token=579B4051EF86407B82CC5E2AF9434F8B
要获取access_token的值,有如下方法:
public static void main(String[] args){
String str="access_token=7BBFBDAE62CA6B9EC0F4B4E810F1C38C&expires_in=7776000&refresh_token=579B4051EF86407B82CC5E2AF9434F8B";
System.out.println(str.substring(str.indexOf("=")+1, str.indexOf("&")));
}
顺便补充说明字符串中子串的查找方法:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。