Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java在字符串中查找URL并创建HTML链接标记_Java_Html - Fatal编程技术网

Java在字符串中查找URL并创建HTML链接标记

Java在字符串中查找URL并创建HTML链接标记,java,html,Java,Html,在我的字符串中,我有一个简单的结果: content = "Hello world, visit this link: www.stackoverflow.com"; HtmlContent = "Hello world, visit this link: <a href="http://stackoverflow.com">http://stackoverflow.com </a> "; 或 或 现在我想在这个字符串中找到url,并最终创建html链接标记,如下所示

在我的字符串中,我有一个简单的结果:

content = "Hello world, visit this link: www.stackoverflow.com";
HtmlContent = "Hello world, visit this link: <a href="http://stackoverflow.com">http://stackoverflow.com </a> ";

现在我想在这个字符串中找到url,并最终创建html链接标记,如下所示:

content = "Hello world, visit this link: www.stackoverflow.com";
HtmlContent = "Hello world, visit this link: <a href="http://stackoverflow.com">http://stackoverflow.com </a> ";
HtmlContent=“你好,世界,请访问此链接:”;
如何得到这个结果

HtmlContent = "Hello world, visit this link: <a href="[http://|www.] stackoverflow.com">[http://|www.]stackoverflow.com </a> ";
HtmlContent=“你好,世界,请访问此链接:”;

我会将句子分成一个数组,检查每个数组中是否有
www
http

string[] possibleLinks = content.split(" ");
for (int i = 0; i < possibleLinks.length; i++)
    if (possibleLinks[i].contains("http://") || possibleLinks[i].contains("www."))
    {
        //Use the link here, which will be posibleLinks[i]
    }
string[]possibleLink=content.split(“”);
for(int i=0;i
由于您确实希望修改现有字符串,因此可以尝试以下操作:

String content = "Hello World! Visit: http://www.lol.com";
int a = 0, b = 0;

a = content.contains("http://") ? content.indexOf("http://") : content.indexOf("www.");
b = content.indexOf(".com") + 4;

if (a != -1)
{
    String link = content.substring(a,b);
    content = content.substring(0,a) + "<a href=\"" + link + "\"/>" + link + "</a>";
}

System.out.println(content);
String content=“你好,世界!访问:http://www.lol.com";
int a=0,b=0;
a=content.contains(“http://”)?content.indexOf(“http:/”):content.indexOf(“www.”);
b=content.indexOf(“.com”)+4;
如果(a!=-1)
{
字符串链接=内容。子字符串(a,b);
content=content.substring(0,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
        }
    }
String content=“你好,请访问此链接:www.stackoverflow.com”;
String[]splitted=content.split(“”);
对于(int i=0;i

在字符串中使用\,在字符串中写上引号。

内容拆分为:

String[] tokens = content.split(" ");
循环遍历
令牌
,并使用正则表达式标识令牌是否为有效url


我想为
www.sample.com
创建html链接标记,例如
。我不想用字符串解析html。@andbee好的,我已经更新了我的答案,请尝试一下谢谢先生。我得到以下错误:
错误:(210,51)java:正则表达式值的非法转义字符
for (int i = 0; i < tokens.length; i++){
    String regex = "^(https?:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?$";

    // if valid match replace this token with desired string. as in your case:
    if(Pattern.matches(regex, tokens[i])){
        tokens[i] = "<a href='"+tokens[i]+"'>"+tokens[i]+"</a>";
    }
}
StringBuilder sbStr = new StringBuilder();
for (int i = 0, i = tokens.length; i++) {
    if (i > 0)
        sbStr.append(" ");
    sbStr.append(aArr[i]);
}

System.out.println(sbStr.toString()); // your expected result.