• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How i can check an string to be a correct web site format?

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
i want to check an string to see whether it is a correct web site name or not.
I know that i should use regular expression , but i do not know how i should do this , can some one help me with this ?

Thanks
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a regular expression.

However, I think it would be better to try to construct a java.net.URL object from the String. If it its a valid URL, the construction will succeed. If it is badly-formed, the constructor will throw MalformedURLException.

That will be good enough to detect many common errors in your URL. However, you may wish to go further. If the java.net.URL object is successfully constructed, you can then check whether the protocol is appropriate for a Web site. You can check this with the getProtocol() method; the protocol should be "http". You could also check that a host has been given, by testing whether the getHost() method returns a non-null, non-empty string.
 
reply
    Bookmark Topic Watch Topic
  • New Topic