I want to get the word "test" from following URL. how can i get it in Java.
http://google.com/test/index.htm
I may or may not know the word "test"
I have tried following:-
Method1
String[] p = s.split("/"); System.out.println("test" + p[0]); System.out.println("test" + p[1]);
o/p:-
http:/
http://google.com/test/index.htm
Try to split it and return n-2 value like:
String[] myUrlSplits = string.split("/");
if (myUrlSplits.length > 2) {
return myUrlSplits[myUrlSplits.length - 2];
}
//throw exception or return default value.
您必须将字符串用“ /”分隔
"http://google.com/test/index.htm".split("/")[3]
String url = "http://google.com/test/index.htm";
System.out.println(url.split("/")[3]);
Update: when you split using "/" character you'll get an array of String the element you want is the fourth one, as the first index of the array is zero, 3 will give you the test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.