C#将变量放在字符串中,并在代码中使用双引号

时间:2016-06-23 10:20:39

标签: c# asp.net

我想在这样的代码中建立链接

"<href=officeclassification?id=" + this.office_id + "&classification=" + "" this.office_classification + ""+>"

office_classification包含2个单词 超级使用 超级干净 我页面中的链接以这种方式显示

officeclassification?id=1&classification=super

但我希望它是

officeclassification?id=1&classification=super used
officeclassification?id=1&classification=super clean

我该怎么做?

3 个答案:

答案 0 :(得分:1)

虽然专注于string.Format的答案可能会使构建字符串变得更容易,但他们并没有解决office_classification内部空间的问题。

为此,您需要HttpServerUtility.UrlEncode,它在asp.net webforms中显示为Server属性。这将以URL安全的方式编码有问题的字符。

string anchor = String.Format("<a href=\"officeclassification?id={0}&classification={1}\">", 
   this.office_id, 
   Server.UrlEncode(this.office_classification));

答案 1 :(得分:0)

试试这个:

string.Format("officeclassification?id={0}&classification={1}", 
  this.office_id, this.office_classification)

答案 2 :(得分:0)

您可以使用

string.Format("your text {0} and more text {1}", var1, var2)

{}是您希望在上面双引号之间的字符串中显示的变量的占位符。

说我想说"My dog {0} is {1} years old.",我可以使用

string name = "Rufus";
int age 2;

Console.WriteLine("My dog {0} is {1} years old.", name, age);

,将打印:

我的狗Rufus已经2岁了。