2013-05-29 19 views
0

我需要用以下URL中的值替换“全名”。 “全名”是一个预定义的字符串,它给出了一个动态值。我需要帮助,在C#中如何做到这一点?如何连接C#中的URL中的字符串?

例如听到的全名= XYZ,我想contacte varible

string FullName="Mansinh"; 
string html = @"<a style=""width:100%%25;height:100%%25"" href=""http://kcs.kayako.com/visitor/index.php?/LiveChat/Chat/Request/_sessionID=34mh1inqnaeliioe3og5tious2t93ip9/_proactive=0/_filterDepartmentID=/_randomNumber=43/_fullName=XYZ/_email=usha%40kcspl.co.in/_promptType=chat"" target=""_blank""> <image style=""width:1340px;height:800px"" src=""/Images/1x1-pixel.png"" /> </a>"; 

回答

1

字符串的html = @“http://kcs.kayako.com/visitor/index.php?/LiveChat/Chat/Request/_sessionID=34mh1inqnaeliioe3og5tious2t93ip9/_proactive=0/_filterDepartmentID=/_randomNumber=43/_fullName= “

+任意字符串你想要+

”/_email=usha%40kcspl.co.in/_promptType=chat“” TARGET = “” _空白 “”>“;

1

使用+运算符连接字符串。例如:

string html = "asdf" + variable + "asdf"; 

记住变量还后使用@上的文字串,当您连接变量为@分隔字符串:

string html = @"asdf" + variable + @"asdf"; 

随着你的字符串:

string html = @"<a style=""width:100%%25;height:100%%25"" href=""http://kcs.kayako.com/visitor/index.php?/LiveChat/Chat/Request/_sessionID=34mh1inqnaeliioe3og5tious2t93ip9/_proactive=0/_filterDepartmentID=/_randomNumber=43/_fullName=" + FullName + @"/_email=usha%40kcspl.co.in/_promptType=chat"" target=""_blank""> <image style=""width:1340px;height:800px"" src=""/Images/1x1-pixel.png"" /> </a>"; 
5

使用StringBuilder或简单情况下使用+运算符。

StringBuilder sb = new StringBuilder() 
sb.Append("The start of the string"); 
sb.Append(theFullNameVariable); 
sb.Append("the end of the string"); 
string fullUrl = sb.ToString(); 

或者

string fullUrl = "The start" + theFullNameVariable + "the end"; 

有性能损失使用+,特别是如果你使用的是它在几个语句而不是一个。在我的实验中,我发现在大约六个连接之后,使用StringBuilder会更快。因人而异

+1

+1提到StringBuilder的只有在一串字符串后才更好 –