1

How can I return a href within a string, I can access the start position but not sure how to get last position :

Here is what I have so far :

    String str = "sadf ad fas dfa http://www.google.com sdfa sadf as dfas";

    int index = str.indexOf("http");
    String href = str.substring(index , ???);

What should the end index be ?

Note, this is targeted at j2me & I need to minimise download footprint so I cannot use regular expressions or third party regular expressions libraries.

michael aubert
  • 6,836
  • 1
  • 16
  • 32
user701254
  • 3,935
  • 7
  • 42
  • 53

3 Answers3

1

Your best bet is probably to cut on the closest space to index or up to the end of the string if no spaces were found.

Omar Al-Ithawi
  • 4,988
  • 5
  • 36
  • 47
0

The only way to do this reliably is to use Regular Expressions. In Java SE, you can do something like this:

Pattern p = Pattern.compile("a*b"); // This is the Regular Expression to match
Matcher m = MY_PATTERN.matcher("aaaaaaab"); // This is the String to search through
while (m.find()) { // While there are matches found
    String s = m.group(1);  // Set String s equal to matches of the entire regex
    // s now contains "aaaaaaab"
}

Groups are basically parts of the regular expression. If you surround parts of it with parentheses it creates a "group" and you can selectively access parts of a string that match only that "part" or "group" of the regular expression.

In J2ME you will need to import a regex library like this one:

http://code.google.com/p/regexp-me/

It's the same overall concept with slightly different syntax

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Using a regular expression library could be very inefficient if that's the only place you need it in your application.

Finding the index of the next space character may be good enough in practice for your current use case but it is not a complete future-proof solution.

What you can do is write a method that extracts all URIs from a String according to the standard definition of what a URI is: http://www.ietf.org/rfc/rfc2396.txt

(In practice, that means looking for special characters that form specific areas of a URI and for characters that are not allowed to be in a URI)

That is also how you may realize that you should actually be looking for the index of "http://", not just "http"

michael aubert
  • 6,836
  • 1
  • 16
  • 32