2hamed
2hamed

Reputation: 9047

How to extract a url from a string in Java?

I have a string containing a short-code which looks like the one below:

some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...

I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).

Upvotes: 0

Views: 3073

Answers (5)

cl-r
cl-r

Reputation: 1264

A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

try as:

   String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
    if (str.contains("url=\""))
    {
        int indexoff = str.indexOf("url=\"");
        int indexofff = str.indexOf("\"]");
        String strurl = str.substring(indexoff, indexofff - indexoff);
        strurl = strurl.Replace("url=\"", ""); //get url string here

    }

Upvotes: 1

Ria
Ria

Reputation: 10367

use this regex for checking and grabbing url:

\[\w+\s+url="(?<urllink>)[^"]*"\s*]

and get gorup named urllink

Upvotes: 2

NimChimpsky
NimChimpsky

Reputation: 47280

String url = "whatever"
Boolean myBool = url.contains("ate");

String contains. Not sure what extract url means, but the string class has lots of useful functions.

Upvotes: 0

Related Questions