string operation

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.
[219 byte] By [@debuga] at [2007-11-27 8:07:15]
# 1
> ...> Will the startsWith() method of string class work> here ?Probably. Have you tried it?
prometheuzza at 2007-7-12 19:49:56 > top of Java-index,Java Essentials,New To Java...
# 2

> 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.

deAppela at 2007-7-12 19:49:56 > top of Java-index,Java Essentials,New To Java...
# 3
You could use this:String httpLink = " http://";urlString = urlString.replace(httpLink, "");urlString = httpLink + urlString;Does it work?
Bruno_Grassellia at 2007-7-12 19:49:56 > top of Java-index,Java Essentials,New To Java...
# 4
> You could use this:Why?@Op. yes you can use startsWith()
kajbja at 2007-7-12 19:49:56 > top of Java-index,Java Essentials,New To Java...
# 5

> 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!

prometheuzza at 2007-7-12 19:49:56 > top of Java-index,Java Essentials,New To Java...
# 6

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

@debuga at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 7
String str="www.sun.com";if( !(str.startsWith("http"))==true){str=" http://"+str;System.out.println(str);}//end of iftake careMohammed Jubaer Arif
Jubaera at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 8
Hi @debug you may want to put more checks in for example if the user misses a / output would be: http://http:/www.url.com
ita6cgra at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 9

> if( !(str.startsWith("http"))==true){

Why making it that complicated?

Either use

if(!str.startsWith("http"))

or

if(str.startsWith("http") == false)

kajbja at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 10

private static final String HTTP_PREFIX = "http://";

...

public String getFixedUrl(String url) {

return ( url.startsWith(HTTP_PREFIX) ? url : HTTP_PREFIX + url);

}

georgemca at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 11
Hi,@ita6cgr: If the user makes such mistake then I throw an error saying invalid url . He does have the option of correcting it in my applicaiton.thanks for pointing it out ..regards,@debug.
@debuga at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...
# 12
No problem :-)
ita6cgra at 2007-7-12 19:49:57 > top of Java-index,Java Essentials,New To Java...