如何从字符串中找到锚 Link?
How find anchor Link from String?
我是 Android 的新手,现在想知道如何从字符串中找到锚点 link 标签,例如我有这样的字符串,它有一些差异 link
string product_distription="Buy this awesome " Thumb Design Mobile OK Stand Holder Universal For All
here input
<a href="http://whosebug.com" >Buy now</a>
"
ouput: http://whosebug.com
现在只想从此字符串中提取 link,因为我的应用程序具有来自 PHP 和 MySQL 的描述 link 并显示在 Android 带有 link 的 textview 所以现在我只想知道是否包含任何 HTML 锚标记的 discrption 它将从 discrption 中提取只能提取而不是整个 discrption 仅显示此 link
fun getLinkFromString() {
val content = "visit this link: www.google.com"
val splitted = content.split(" ")
for (i in splitted.indices) {
if (splitted[i].contains("www.") || splitted[i].contains("http://")) {
println(splitted[i]) //just checking the output
val link = "<a href=\"" + splitted[i] + "\">" + splitted[i] + "</a>"
println(link)
Toast.makeText(this, link, Toast.LENGTH_LONG).show()
}
}
}
这可能会帮助你实现类似的。
您可以按照以下方式进行:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(extractAnchorLinks(
"This <a href=\"www.google.com\">search engine</a> is the most popular. This <a href=\"www.whosebug.com\"> website is the largest online community for developers</a>There are millions of websites today"));
}
public static List<String> extractAnchorLinks(String string) {
List<String> anchorLinkList = new ArrayList<String>();
final String TAG = "a href=\"";
final int TAG_LENGTH = TAG.length();
int startIndex = 0, endIndex = 0;
String nextSubstring = "";
do {
startIndex = string.indexOf(TAG);
if (startIndex != -1) {
nextSubstring = string.substring(startIndex + TAG_LENGTH);
endIndex = nextSubstring.indexOf("\">");
if (endIndex != -1) {
anchorLinkList.add(nextSubstring.substring(0, endIndex));
}
string = nextSubstring;
}
} while (startIndex != -1 && endIndex != -1);
return anchorLinkList;
}
}
输出:
[www.google.com, www.whosebug.com]
逻辑很简单。此外,变量名也是自解释的。不过,如有任何疑问,请随时发表评论。
我是 Android 的新手,现在想知道如何从字符串中找到锚点 link 标签,例如我有这样的字符串,它有一些差异 link
string product_distription="Buy this awesome " Thumb Design Mobile OK Stand Holder Universal For All
here input
<a href="http://whosebug.com" >Buy now</a>
"
ouput: http://whosebug.com
现在只想从此字符串中提取 link,因为我的应用程序具有来自 PHP 和 MySQL 的描述 link 并显示在 Android 带有 link 的 textview 所以现在我只想知道是否包含任何 HTML 锚标记的 discrption 它将从 discrption 中提取只能提取而不是整个 discrption 仅显示此 link
fun getLinkFromString() {
val content = "visit this link: www.google.com"
val splitted = content.split(" ")
for (i in splitted.indices) {
if (splitted[i].contains("www.") || splitted[i].contains("http://")) {
println(splitted[i]) //just checking the output
val link = "<a href=\"" + splitted[i] + "\">" + splitted[i] + "</a>"
println(link)
Toast.makeText(this, link, Toast.LENGTH_LONG).show()
}
}
}
这可能会帮助你实现类似的。
您可以按照以下方式进行:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(extractAnchorLinks(
"This <a href=\"www.google.com\">search engine</a> is the most popular. This <a href=\"www.whosebug.com\"> website is the largest online community for developers</a>There are millions of websites today"));
}
public static List<String> extractAnchorLinks(String string) {
List<String> anchorLinkList = new ArrayList<String>();
final String TAG = "a href=\"";
final int TAG_LENGTH = TAG.length();
int startIndex = 0, endIndex = 0;
String nextSubstring = "";
do {
startIndex = string.indexOf(TAG);
if (startIndex != -1) {
nextSubstring = string.substring(startIndex + TAG_LENGTH);
endIndex = nextSubstring.indexOf("\">");
if (endIndex != -1) {
anchorLinkList.add(nextSubstring.substring(0, endIndex));
}
string = nextSubstring;
}
} while (startIndex != -1 && endIndex != -1);
return anchorLinkList;
}
}
输出:
[www.google.com, www.whosebug.com]
逻辑很简单。此外,变量名也是自解释的。不过,如有任何疑问,请随时发表评论。