1
比方說,我們有兩個字符串(ULRs):字符的第N次出現,並且如果條件語句
https://stackoverflow.com/questions/ask
https://web.whatsapp.com/
我需要這樣來寫表達式:
如果第3斜線後( /)是沒有或第三斜線不存在做
{
some operation
} else {
another action
}
請幫助。
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
//copying and removing https from the list
List<String> list2 = new ArrayList<String>();
Collections.addAll(list2, stringArr);
Iterator<String> iter = list2.iterator();
while(iter.hasNext()){
if(iter.next().contains(https))
// here you should copy https lines to another file.
iter.remove();
}
String[] stringArr2 = list2.toArray(new String[0]);
for (int i = 0; i<stringArr2.length; i++) {
//condition for pure domain names.
//else the action below
System.out.println(getDomainName(stringArr2[i]) + "," + stringArr2[i] + "," + "U" +"," + number_of_doc + "," + today);
}
}
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
}
如果使用番石榴的'Splitter',你甚至可以指定一個上限您得到令牌的數量,所以你可以以後檢索所有內容第三個'/'作爲單個字符串。 – biziclop
我看到這是個好主意@biziclop謝謝你,你的意思是這個'Splitter.on(CharMatcher.anyOf(「/」))。omitEmptyStrings()。split(「https://stackoverflow.com/questions/ask」 )' –
差不多,你只需要不要調用'omitEmptyStrings()',而是調用'limit(4)'。這樣返回的第四個元素就是'questions/ask'。如果你得到的元素少於4個,你的字符串中沒有3'/'s。 – biziclop