[英]Get the specific value from the last part of a String in Android
I wanted to get the value from the last part of a String, here's my example String 我想从字符串的最后一部分获取值,这是我的示例字符串
String str="www.mywebsite.com?id=0001&user=myname" 字符串str =“ www.mywebsite.com?id=0001&user=myname”
I like to get the word myname from that String, All examples that I'm seeing is like this 我喜欢从字符串中获取单词myname ,我看到的所有示例都是这样
String getUser = str.substring(str.length() - 6);
but user value length changes every transaction so I can't fix that to any value. 但是用户价值长度会改变每笔交易,因此我无法将其固定为任何价值。 Can anyone please help me in how I will be able to get the user value from that String. 谁能帮我从该字符串中获取用户价值。 Thanks. 谢谢。
String getUser=str.subString(str.lastIndexOf("=")+1,Str.length());
将返回我的名字。
If your string is going to be a uri, you can use Uri's getQueryParameter: 如果您的字符串将是uri,则可以使用Uri的getQueryParameter:
String str="www.mywebsite.com?id=0001&user=myname";
Uri uri = Uri.parse(str);
return uri.getQueryParameter("user");
尝试这个
String getUser = str.substring(str.lastIndexOf("=") + 1);
Try this 尝试这个
String foo = "www.mywebsite.com?id=0001&user=myname";
String[] split = foo.split("=");
String name = split[split.length-1];
with String.split(), you can split strings with a delimiter and put the values in an array. 使用String.split(),您可以使用定界符分割字符串并将值放入数组中。 It is similar to PHPs' explode method. 它类似于PHP的explode方法。
String str="www.mywebsite.com?id=0001&user=myname";
String user = getQueryParamValue(str);
public static final String getQueryParam(String url) {
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params){
if("user".equalsIgnoreCase(param.getName())) {
return param.getValue();
}
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.