在Java中添加if语句
我想确保URL中的某个地方有一个/,如果最后没有,它会添加一个,例如:www.google.co.uk变为www.google.co.uk/在Java中添加if语句,java,string,Java,String,我想确保URL中的某个地方有一个/,如果最后没有,它会添加一个,例如:www.google.co.uk变为www.google.co.uk/ 我如何在URL末尾添加/如果必要?如果!URL.endsWith/{URL=URL+/;}似乎太明显了 我发现很难相信你正在做的字符串替换实际上将sambulosenda.com更改为tink.stca.herts.ac.uk。。。 //loop for each argument on the command line for(int argind=0;
我如何在URL末尾添加/如果必要?如果!URL.endsWith/{URL=URL+/;}似乎太明显了 我发现很难相信你正在做的字符串替换实际上将sambulosenda.com更改为tink.stca.herts.ac.uk。。。
//loop for each argument on the command line
for(int argind=0; argind<args.length; argind++) {
// each argument is a URL to fetch
String URL = args[argind];
String fileName = null;
int port = 80; // default, unless URL contains :nnnn
// first check that it is an HTTP URL
if(! URL.startsWith("http://")) {
System.err.println(URL+" does not start with http:\n");
continue;
}
// remove "http://" so that for example:
// http://xxxxx/stuff/tiny.html
// becomes:
// xxxxxx/stuff/tiny.html
URL = URL.replace("http://", "");
}
//loop for each argument on the command line
for(int argind=0; argind<args.length; argind++) {
// each argument is a URL to fetch
String URL = args[argind];
String fileName = null;
int port = 80; // default, unless URL contains :nnnn
// removes any blank spaces at the start and the end of the string
URL = URL.trim();
// first check that it is an HTTP URL
if(! URL.startsWith("http://")) {
System.err.println(URL+" does not start with http:\n");
continue;
}
// remove "http://" so that for example:
// http://sambulosenda.com/stuff/tiny.html
// becomes:
// sambulosenda.com/stuff/tiny.html
URL = URL.replace("http://", "");
// checks if the url contains the / at the end
If (!URL.endsWith("/")
{
URL = URL.concat("/"); // if not then one is added
}