2015-04-08 259 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. 

这是否回答你的问题?