Closed. This question needs details or clarity 。它目前不接受答案。












想改善这个问题吗?添加细节并通过 editing this post 澄清问题。

6年前关闭。



Improve this question




在我的字符串中,我只有这个结果:
content = "Hello world, visit this link: www.stackoverflow.com";

或者
content = "Hello world, visit this link: http://www.stackoverflow.com";

或者
content = "Hello world, visit this link: http://stackoverflow.com";

现在我想在这个字符串中找到 url 并最终创建 html 链接标签,如下所示:
HtmlContent = "Hello world, visit this link: <a href="http://stackoverflow.com">http://stackoverflow.com </a> ";

如何得到这个结果?
HtmlContent = "Hello world, visit this link: <a href="[http://|www.] stackoverflow.com">[http://|www.]stackoverflow.com </a> ";

最佳答案

你可以这样试试:

String content = "Hello world, visit this link: www.stackoverflow.com";

    String[] splitted = content.split(" ");
    for (int i = 0; i < splitted.length; i++) {
        if ((splitted[i]).contains("www.")) { // use more statements for
                                                // http:// etc..
            System.out.println(splitted[i]); //just checking the output
            String link = "<a href=\"" + splitted[i] + "\">" + splitted[i] + "</a>";

            System.out.println(link); //just checking the output
        }
    }

在字符串中使用\"在字符串中写入引号。

关于Java 在字符串中查找 URL 并创建 HTML 链接标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27245669/

10-08 22:52