2013-05-29 21 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的只有在一串字符串後才更好 –