[英]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.