Reputation: 37733
I have this string:
"http://my/website/collections/index.php?s=1&schema=http:/my/web/fe7cd50991b11f51050902sddaf3e042bd5467/idApp=19"
I want to extract this token from the string: fe7cd50991b11f51050902sddaf3e042bd5467
the website can vary, but the only think cannot vary is that the string token i must obtain always is on the left of "/idApp="
Which is the most efficient way to solve that?
thanks.
Upvotes: 0
Views: 142
Reputation: 9
You could use the regex
These two packages will help you
java.util.regex.Matcher
java.util.regex.Pattern
Upvotes: 0
Reputation: 7955
You don't need regexp here. Absolutely. The task is just to cut a piece of string, don't over-complicate. Simplicity is the key.
int appIdPosition = url.lastIndexOf("/idApp=");
int slashBeforePosition = url.lastIndexOf("/", appIdPosition - 1);
String token = url.substring(slashBeforePosition + 1, appIdPosition);
Upvotes: 1
Reputation: 1214
When doing anything with strings, always look to:
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html
Here is my answer...
public static void main(String[] args) {
//Don't forget: import static org.apache.commons.lang.StringUtils.*;
String url2 = "http://my/website/collections/index.php?s=1&schema=http:/my/web/fe7cd50991b11f51050902sddaf3e042bd5467/idApp=19";
String result = substringAfterLast("/", substringBeforeLast(url2,"/")) ;
System.out.println(result);
}
Upvotes: 0
Reputation: 13717
Simple 2 times split would work for multiple parameters. First split on "idApp"
and then on /
.
The following code would work even if there are multiple parameters after the idApp
parameter.
String url = "http://my/website/collections/index.php?s=1&schema=http:/my/web/fe7cd50991b11f51050902sddaf3e042bd5467/idApp=19";
String[] tokens = url.split("idApp");
String[] leftPartTokens = tokens[0].split("/");
String searched = leftPartTokens[leftPartTokens.length - 1];
System.out.println(searched);
Upvotes: 0
Reputation: 5090
Assuming the token can only numbers and letters, you can use something like this.
It matches a sequence of numbers and letters, preceding the /idApp= string.
It is "efficient" in terms of being a standard, easy-to-read way to do that, but there may be more performance-efficient ways to do it, although you should think carefully about whether finding this string would really be a performance bottle-neck.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegexp {
public static void main(String args[]) {
String text = "http://my/website/collections/index.php?s=1&schema=http:/my/web/fe7cd50991b11f51050902sddaf3e042bd5467/idApp=19";
Pattern pattern = Pattern.compile("(\\w+)/idApp=");
Matcher m = pattern.matcher(text);
if (m.find()) {
System.out.println(m.group(1));
}
}
}
Upvotes: 2
Reputation: 3608
String url = "http://my/website/collections/index.php?s=1&schema=http:/my/web/fe7cd50991b11f51050902sddaf3e042bd5467/idApp=19";
String[] tokens = url.split("/");
String searched = tokens[array.length - 2];
This will work if the token is everytime the prelast. Otherwise you need to go through the Array
and check if the current token matches your condition and take the token before.
In code:
int tokenId = 0;
for (int i = 0; i < tokens.length; i++) {
if (token[i].equals("/idApp=")) {
tokenId = i - 1;
break;
}
}
String rightToken = tokens[tokenId];
Upvotes: 3