如何在 java 中按单词拆分
How to split by word in java
我想从关注 URL 得到单词 "test"。我怎样才能在 Java.
中得到它
http://google.com/test/index.htm
我可能知道也可能不知道这个词"test"
我试过以下方法:-
方法一
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
尝试拆分它和 return 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
我想从关注 URL 得到单词 "test"。我怎样才能在 Java.
中得到它http://google.com/test/index.htm
我可能知道也可能不知道这个词"test"
我试过以下方法:-
方法一
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
尝试拆分它和 return 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