2

If I have the following URL:

http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0

How can I get the name of the plugin (simply named wordpressplugin in the URL) and the version so the output will be - wordpressplugin ver 1.0?

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
Daniel
  • 21
  • 3
  • use the split function – DCR Dec 16 '18 at 14:49
  • where do you want to get this "string" - do you use spring boot or any other mvc framework? - what is your motivation, why do you want to do this. there are multiple ways to do so. you could use regex, if you use an mvc framework, there would be most likely some tools to help you. but in the end, it is always good to post your motivation, because based on your problem, not every solution might fit, without knowing your motivation – Simon Schrottner Dec 16 '18 at 14:56
  • Does the url always has same structure? Is plugin name comes always just after http://www.example.com/wordpress/plugins/ ? – Emre Savcı Dec 16 '18 at 14:58
  • Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Ryuzaki L Dec 16 '18 at 15:17
  • Emre Savcı - I'm pretty sure yes. – Daniel Dec 16 '18 at 15:29

5 Answers5

1

I am posting my comment as an answer

String s = "http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0";
String[] ary = s.split("/"); 
System.out.println(ary[5] + " " + ary[7]); 

Easiest way this is acc to your question, you have to use regex for more dynamic searching.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
  • I haven't tested/verified your answer but I have a little doubt as to whether this would throw an exception or not. `ary[1]` is obviously null. That was why I was careful with that in my own answer. – Taslim Oseni Dec 16 '18 at 15:14
1

You may do it like so, using Regex support in Java.

String url = "http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0";
Pattern pattern = Pattern.compile("(.*plugins/)(.*)(/\\d{3}/)(ver.*)");
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
    System.out.println("Plugin: " + matcher.group(2));
    System.out.println("Version: " + matcher.group(4));
}

Notice the use of capture groups. Here's the output.

Plugin: wordpressplugin
Version: ver=1.0
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
0

You should have a look into Regular Expressions (in Oracle tutorials), which are the general tool in any programming language to get/match sub-strings out of a larger string (which follows some more or less fixed format).

Jaleks
  • 561
  • 5
  • 19
0

Because you claim to be new to JAVA, here is a very simple answer that should suit your skills

String url = "http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0";
String search = "plugins/";
int index = url.indexOf(search);
String pluginName, version;

if (index > -1)
{
    index += search.length;
    pluginName = url.substring(index, url.indexOf("/",index + 1));

    search = "ver=";
    index = url.indexOf(search);

    if (index > -1)
    {
       version = url.substring(index + search.length);

       System.out.prinln(pluginName + " " + version);
    }
}
ymz
  • 6,602
  • 1
  • 20
  • 39
  • 2
    this question is tagged in java ,please rectify it – Shubham Dixit Dec 16 '18 at 15:01
  • Thanks to @Dixit I changed it to JAVA (instead of JS). Use His solution if the url structure is fixed (better and faster solution for this use-case). My approach is longer but also more flexible (if required). – ymz Dec 16 '18 at 15:13
0

PS: This would work if and only if your url format always remains the same!

The fastest way to solve this problem is to take advantage of the split method of Strings. Just study the method below carefully, it's basic.

    public String getVersionNumber(String url){

        String[] arr0 = url.split("//");
        //The code above returns an array of two strings: "http:" and "www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0"

        String[] arr1 = arr0[1].split("/");
        //The code above returns an array of six strings: "www.example.com", "wordpress", "plugins", "wordpressplugin", "123" and "ver=1.0".

        return String.format("%s %s", arr1[3], arr1[5]);
        //OUTPUT: wordpressplugin ver=1.0
        //I simply returned what I needed.
    }

I hope this helps.. merry coding!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69