How to Get the Last Word from a URL in Java

This tutorial is intended for those who are trying to get the last word. Huh? We will learn How to Get the Last Word from a URL in Java. This might come in handy when you are trying to compare a clicked link or a text with the URL. You can grab the last word from the URL and then check if the link you clicked had the word in it or not.

wise man meme for the last word

It is really helpful for Assertions. We are going to make use of substring method and lastIndexOf methods to grab the last word from the URL

How to Get the Last Word from a URL in Java

I am assuming you have a URL with you. I am going to take the following URL as an example:

http://straightfromamovie.com/black-panther-movie-review

That was a good movie btw! Good stuff!

What’s the last word in the above URL? “Review” right?

Let’s see how to grab that in simple steps:

I am assuming that you have your Java class ready to take inputs. I am just going to write my code in the main method for ease of access.

Step 1: Put your URL in  a String variable:

String url = "http://straightfromamovie.com/black-panther-movie-review";

Remember to put the String data inside the double quotes.

Step 2: We will make use of substring and lastIndexOf now. So type the following in your Eclipse:

url.substring(url.lastIndexOf("-") + 1);

Step 3: Assign it to a variable or you can simply choose to display it using System.out. I will do the former.

String last = url.substring(url.lastIndexOf("-") + 1);

Using lastIndexOf method with the parameter basically returns an int value which we use as a parameter for the substring. So if you would have simply used url.substring(24) it would have given a result something like this:

e.com/black-panther-movie-review

So you get it right?

We are simply trying to find the last word using the “-” symbol and then taking everything’s after. +1 is for not including the – symbol. If you don’t add +1 it will include it as well.

So we need to provide the character or symbol from where it should start the substring.

Step 4: Print the word using Sysout.

System.out.println(last);

Here’s how your whole code might appear like:

Full Code for How to Get the Last Word from a URL in Java

Step 5: Run the program by clicking on the green arrow button or Right-Click> Run As> Java Application.

Here’s the result you will get:

review

Neat, right?

If Working on Selenium

If you are working on Selenium and you wish to see how the above can be interpreted simply replace the String url line with the code that grabs the URL:

driver.get("http://straightfromamovie.com/black-panther-movie-review");
String url = driver.getCurrentURL();

Of course I am assuming you have declared and instantiated WebDriver and set System property for it as well before proceeding.

You will get the same result.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply