我希望在斜杠之间检索文本,但在斜杠之前和之后打印文本
public class Divide {
public static void main(String[] args) {
StringBuilder link = new StringBuilder("www.entekhab.ir/fa/news/280809/");
System.out.println(link+"\n");
divideLink(link);
}
static void divideLink (StringBuilder link){
String slash = "/";
String divide = null;
System.out.println("index of '"+slash+"' : ");
for (int i = -1; (i = link.indexOf(slash, i + 1)) != -1; ) {
System.out.print(i+" ");
divide = link.substring(0, i);
System.out.println(divide);
}
}
}
输出如下:
www.entekhab.ir/fa/news/280809/
index of '/' :
15 www.entekhab.ir/
18 www.entekhab.ir/fa/
23 www.entekhab.ir/fa/news/
30 www.entekhab.ir/fa/news/280809/
我需要这样的输出:
www.entekhab.ir/fa/news/280809/
index of '/' :
15 www.entekhab.ir
18 fa
23 news
30 280809
答案 0 :(得分:0)
public static void divideLink(String link) {
System.out.println("index of '/':");
int i = 0;
for (String part : link.split("/")) {
System.out.println((i += part.length()) + " " + part);
i++;
}
}