how to check protocol is present in URL , if not present need to append it.
is there any class to achieve this in java?
eg: String URL = http://www.google.com
need to get http://www.google.com
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s say you have
String url = www.google.com. String class methods would be enough for the goal of checking protocol identifiers. For example,url.startsWith("https://")would check whether a specific string is starting with the given protocol name.However, are these controls enough for validation?
I think they aren’t enough. First of all, you should define a list of valid protocol identifiers, e.g. a String array like
{"http", "ftp", "https", ...}. Then you can parse your input String with regex("://")and test your URL header whether it belongs to the list of valid protocol identifiers. And domain name validation methods are beyond this question, you can/should handle it with different techniques as well.