2013-05-30 48 views
3
String s= 
    "<html> 
    <head> 
    <title>app</title> 
    </head> 
    <body bgcolor="pink"> 
    <div align="center"><img src="" width="900" height="100"/> 
    </div> 
    <h2 align="center">Welcome to your rewards program</h2> 
    <table> 
    <tr> 
    <td><p>&nbsp;&nbsp;&nbsp;&nbsp;You now have ongoing uninterrupted access to thousands of discounts all on your phone .Accessing great savings just got so easier . You can search by category , by keyword and also search local with what around me. Select a merchant offer you like and present the coupon on your phone at the time of settling your bill . Some merchants only offer their services online and you will also be able to click through to their online offerings . 
    GIN image</p></td> 
    </tr> 

    </table> 
    </body> 
    </html>"; 

我想将html数据添加到字符串,但它给了我错误。我该如何解决这个问题? 我使用了escapse序列,但它没有奏效。如何用双引号将HTML数据添加到java中的字符串?

感谢

+0

可以从Apache使用[StringEscapeUtils(http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html)? – NINCOMPOOP

+0

你不能在java中打开多行字符串。你必须用+连接它们,然后跳过“当然 –

回答

4

试试这个

String s= 
      "<html>"+ 
      "<head>"+ 
      "<title>app</title>"+ 
      "</head>"+ 
      "<body bgcolor=\"pink\">"+ 
      "<div align=\"center\"><img src=\"\" width=\"900\" height=\"100\"/>"+ 
      "</div>"+ 
      "<h2 align=\"center\">Welcome to your rewards program</h2>"+ 
      "<table>"+ 
      "<tr>"+ 
      "<td><p>&nbsp;&nbsp;&nbsp;&nbsp;You now have ongoing uninterrupted access to thousands of discounts all on your phone .Accessing great savings just got so easier . You can search by category , by keyword and also search local with what around me. Select a merchant offer you like and present the coupon on your phone at the time of settling your bill . Some merchants only offer their services online and you will also be able to click through to their online offerings ."+ 
      "GIN image</p></td>"+ 
      "</tr>"+ 
      "</table>"+ 
      "</body>"+ 
      "</html>"; 
+0

它为我工作。谢谢你这么多 – user2376732

0

如果您使用的是Eclipse IDE ...

Eclipse将不支持多行字符串赋值。

如果你正在使用Eclipse编辑器,则跳转 Windows -> Preferences -> Java -> Editor -> Typing 和焯芬Escape text when pasting into a string literal

之后,你可以复制整个字符串值和过去的这一次。

1
String s= 
     "<html>" 
     + "<head>" 
     + "<title>app</title>" 
     + "</head>" 
     + "<body bgcolor=\"pink\">" 
     + "<div align=\"center\"><img src=\"\" width=\"900\" height=\"100\"/>" 
     + "</div>" 
     + "<h2 align=\"center\">Welcome to your rewards program</h2>" 
     + "<table>" 
     + "<tr>" 
     + "<td><p>&nbsp;&nbsp;&nbsp;&nbsp;You now have ongoing uninterrupted access to thousands of discounts all on your phone." 
     + " Accessing great savings just got so easier . You can search by category , by keyword and also search local with what around me. Select a merchant offer you like and present the coupon on your phone at the time of settling your bill." 
     + " Some merchants only offer their services online and you will also be able to click through to their online offerings ." 
     + " GIN image</p></td>" 
     + "</tr>" 
     + "</table>" 
     + "</body>" 
     + "</html>"; 
+0

在上面的代码中整个字符串将是一行来格式化它添加一个+”\ n“每行 – prvn

相关问题