[英]Output urls containing some words from a string to another string java
我有一个包含几个网址的字符串。 我想检索包含“ thistext”的URL,并将它们复制到另一个名为“ outputlinks”的变量中。 这是我的代码。
String links = "http://www.website1.com/thistext
http://www.website1.com/othertext
http://www.website1.com/thistext";
String outputlinks =""; // ??
首先,为您的链接使用良好的分隔符,例如commah,分号或其他内容。 使用字符串标记器获取各个链接,最后使用indexOf
搜索您的术语,即每个标记字符串中的“ thisText”。 那将为您完成工作。
假设您的意思是:
String links = "http://www.website1.com/thistext\n" +
"http://www.website1.com/othertext\n" +
"http://www.website1.com/thistext";
然后,您可以执行以下操作:
public static void main(String[] args) {
String links = "http://www.website1.com/thistext\n" +
"http://www.website1.com/othertext\n" +
"http://www.website1.com/thistext";
String[] linksArray = links.split("\n");
for (String link : linksArray) {
if (link.contains("thistext")) {
System.out.println(link);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.