> Hi,
>
> I have an url and I want to append http:// in the
> beginning if the user forgets to enter it. How do I
> do this ?
>
> Will the startsWith() method of string class work
> here ?
>
> thanks,
>
> @debug.
you can do this:
String httpLink = "http://";
add httpLink in front of whatever you want. ( System.out.println(httpLink + "your link); )
The problem is that the user doesn't need to typ http:// because it will add it automagically. But im sure you can think of a solution.
> You could use this:
>
> String httpLink = "http://";
> urlString = urlString.replace(httpLink, "");
> urlString = httpLink + urlString;
>
> Does it work?
No because "http://" can be a valid part inside a URL. And with replace("http://",""), you will remove those too!
Hi,
Thanks for the replies I solved the problem with startsWith in the following way:
geturl is a string that stores the url.
if(!geturl.startsWith("http://"))
{
geturl = "http://"+ geturl;
}
regards,
@debug
> if( !(str.startsWith("http"))==true){
Why making it that complicated?
Either use
if(!str.startsWith("http"))
or
if(str.startsWith("http") == false)
private static final String HTTP_PREFIX = "http://";
...
public String getFixedUrl(String url) {
return ( url.startsWith(HTTP_PREFIX) ? url : HTTP_PREFIX + url);
}