[英]How to split by word in java
我想从以下URL中获得单词“ test”。 我如何在Java中获得它。
http://google.com/test/index.htm
我可能会或可能不会知道“测试”一词
我试过以下:
方法1
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
尝试将其拆分并返回n-2值,例如:
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]);
更新:当您使用“ /”字符进行拆分时,您将获得一个String数组,所需元素是第四个元素,因为该数组的第一个索引为零,所以3将为您提供test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.