I have the below for loop which I use to append a number to the end of a URL String:
我有以下for循环,我用它在URL字符串的末尾附加一个数字:
for(int i = 0; i < 5; i++){
PUT_URL = PUT_URL + i;
System.out.println(PUT_URL);
sendPUT();
System.out.println("PUT Done");
}
Currently the url appears in the format:
目前,网址以以下格式显示:
myurl1
myurl12
myurl123
myurl1234
myurl12345
What's the best way to amend this so the url appears as?
修改此内容的最佳方法是什么,以便网址显示为?
myurl1
myurl2
myurl3
myurl4
myurl5
1 个解决方案
#1
2
With this line
有了这条线
PUT_URL = PUT_URL + i;
you are modifying PUT_URL
, that I presume contains myurl
, by appending i
to it and then printing. Therefore in the next iteration PUT_URL
will contain a number at the end and then you are appending the next number.
你正在修改PUT_URL,我认为它包含myurl,通过将i附加到它然后打印。因此,在下一次迭代中,PUT_URL将在末尾包含一个数字,然后您将附加下一个数字。
I would suggest creating a constant with the prefix of a url without the number at the end and then append a number to that to create PUT_URL
:
我建议创建一个带有url前缀的常量,但不包含最后的数字,然后在其中附加一个数字来创建PUT_URL:
String URL_PREFIX = "myurl";
for(int i = 0; i < 5; i++) {
PUT_URL = URL_PREFIX + i + 1;
System.out.println(PUT_URL);
sendPUT();
System.out.println("PUT Done");
}
#1
2
With this line
有了这条线
PUT_URL = PUT_URL + i;
you are modifying PUT_URL
, that I presume contains myurl
, by appending i
to it and then printing. Therefore in the next iteration PUT_URL
will contain a number at the end and then you are appending the next number.
你正在修改PUT_URL,我认为它包含myurl,通过将i附加到它然后打印。因此,在下一次迭代中,PUT_URL将在末尾包含一个数字,然后您将附加下一个数字。
I would suggest creating a constant with the prefix of a url without the number at the end and then append a number to that to create PUT_URL
:
我建议创建一个带有url前缀的常量,但不包含最后的数字,然后在其中附加一个数字来创建PUT_URL:
String URL_PREFIX = "myurl";
for(int i = 0; i < 5; i++) {
PUT_URL = URL_PREFIX + i + 1;
System.out.println(PUT_URL);
sendPUT();
System.out.println("PUT Done");
}