gpt4 book ai didi

java - 将html链接放入java代码中

转载 作者:行者123 更新时间:2023-11-28 04:39:40 27 4
gpt4 key购买 nike

我是 Java 编码的新手。我有一个可以发送邮件的应用程序,是用 Java 实现的。我想在邮件中放置一个 HTML 链接,但是当我放置一个 html 链接时,它会显示一条错误消息:即使一切正确,也丢失了。

String msgbody = "This is a reminder mail"; 
String link = "<a href="http://abcd.efg.com" target="_blank">http://abcd.efg.com</a>";
msgbody = msgbody + link;

如果我使用这样的字符串有什么问题吗?

最佳答案

您需要对字符串文字中的引号进行转义。

代替

String link = "<a href="http://abcd.efg.com" target="_blank">http://abcd.efg.com</a>";

尝试

String link = "<a href=\"http://abcd.efg.com\" target=\"_blank\">http://abcd.efg.com</a>";

有关详细信息,请参阅 "Characters"

Escape Sequences

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:

...

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence

She said "Hello!" to me.

you would write

System.out.println("She said \"Hello!\" to me.");

关于java - 将html链接放入java代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com