2015-04-08 189 views
-1

我已經創建了下面的字符串。創建一個串聯的字符串

The <a target="" href="http://google.com/search=xyz">xyz</a><a target="" href="http://google.com/search=xyz"><img src="http://google.com/small.gif" alt="" title="smallimage" border="0"></a> has all the search info. 

我正在嘗試如下操作。

String X = "The <a target="" href="http://google.com/search=xyz">xyz</a><a target="" href="http://google.com/search=xyz"><img src="http://google.com/small.gif" alt="" title="smallimage" border="0"></a> has all the search info." 

但它是扔我語法錯誤。

這裏我創建了另一個名爲searchTerm的String,如下所示。

String searchTerm = "Hello"; 

我想通過如下所示的串聯來創建最終的字符串。

String X = "The <a target="" href="http://google.com/search="+searchTerm+"">xyz</a><a target="" href="http://google.com/search="+searchTerm+""><img src="http://google.com/small.gif" alt="" title="smallimage" border="0"></a> has all the search info." 

甚至這是行不通的。

請讓我知道我該如何解決這個問題。

在這裏,我甚至感到困惑<and>標籤,不僅是引號

感謝

+0

你想讀取一些html或將它用於其他目的 –

+1

使用'String.format()'更好,更易讀。 –

回答

2

你需要逃避,你要出現在字符串引號。

String x = "<a target=\"...\" href=\"...\">...</a>"; 
+0

那麼這個角色應該沒問題,那麼'<' and '>' – user3872094

+0

怎麼樣。 – hvgotcodes

+0

好的,謝謝! – user3872094

2

這是我寫的......

String s = "The <a target=\"\" href=\"http://google.com/search=xyz\">xyz</a><a target=\"\" href=\"http://google.com/search=xyz\"><img src=\"http://google.com/small.gif\" alt=\"\" title=\"smallimage\" border=\"0\"></a> has all the search info."; 
System.out.println(s); 

和這裏的輸出我得到....

The <a target="" href="http://google.com/search=xyz">xyz</a><a target="" href="http://google.com/search=xyz"><img src="http://google.com/small.gif" alt="" title="smallimage" border="0"></a> has all the search info. 

這是否回答你的問題?