在Java中添加if语句

时间:2014-06-14 06:09:00

标签: java string

//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://", "");
}

我想确保网址中有/某处,以便如果最后没有/它添加一个,那么例如:www.google.co.uk变为www.google.co.uk/

如果在URL末尾有必要,我如何声明添加/

2 个答案:

答案 0 :(得分:4)

if(! URL.endsWith("/") ){ URL=URL+"/";}似乎太明显了。

答案 1 :(得分:0)

//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
  }