2013-05-30 84 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

相關問題