0

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.

vikiiii
  • 9,246
  • 9
  • 49
  • 68
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

6 Answers6

3
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];
tgr
  • 3,557
  • 4
  • 33
  • 63
2

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

    }
}
matt freake
  • 4,877
  • 4
  • 27
  • 56
1

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);
Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
0

You could use the regex

These two packages will help you

  • java.util.regex.Matcher
  • java.util.regex.Pattern
ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • 1
    You might want to elaborate on your answer and provide the OP with a solution, rather than talk about available packages only :) – Sujay Sep 05 '12 at 08:25
0

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);
mtk
  • 13,221
  • 16
  • 72
  • 112
0

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);
}
Nos
  • 1,024
  • 1
  • 8
  • 14
  • Though the answer is completely valid, I wouldn't advise the OP to use static imports. [What is a good use case for static import of methods?](http://stackoverflow.com/q/420791/851811) – Xavi López Sep 05 '12 at 09:01
  • You're correct, the static import is not necessary but in my opinion it keeps the code less verbose and most IDE's will tell us where the code originates from. I normally do a static import of the StringUtils as I would tend to use it heavily and Oracle says "use it when you require frequent access to static members from one or two classes.". – Nos Sep 05 '12 at 10:43