0

A friend of mine requested to write a simple code to check whether a URL ends with asp or not. I am thinking about accepting the URL as string and checking the last tree letters if it's as or not. I just want to check is there any ways to write this code. (of course there are plenty of ways but it need to be simple)

Thanks in advance

2
  • Are you concerned with querystring variables? eg: ../index.asp?article=1455&category=32 Commented May 4, 2011 at 17:49
  • I guess. How can I find a solution for that kind of input Commented May 4, 2011 at 17:51

4 Answers 4

4
String url = ... if(url.endsWith(".asp")){ // do your thing } 
Sign up to request clarification or add additional context in comments.

Comments

2

You will probably want to use the String.endsWith() method.

if (s.endsWith("asp")) { System.out.println("Yes"); } else { System.out.println("No"); } 

If you really want to do this properly, you can parse the string as a real java.net.URL and extract the path using getPath().

URL url = new URL(s); String path = url.getPath(); if (path.endsWith("asp")) { System.out.println("Yes"); } else { System.out.println("No"); } 

3 Comments

Im going to write the output yes or no into a text file. However I cannot think how to build it yet. Also what if the string ends with index.asp?article=123. As Ken mentioned.
@the magnificent: Answer updated to handle things like index.asp?article=123. As for writing to a file, use a FileWriter or just redirect the output of your program.
Thanks Adam.. This looks more logical
0

Use a regular expression:

.*\.asp(x){0,1}(\?){0,1}.* 

This will handle these cases:

Comments

0

A regex would be a solution though I find them extremely ugly in Java.

String url = .... if (Pattern.compile("\.asp[^\.]*\z").matcher(url).matches()){ //do your thing } 

Yuk indeed. And i'm not sure if I got the regex right

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.