在C#中的字符串 [英] string in c#
本文介绍了在C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string customButtonControl = "<asp:Button ID="Button1" runat="server" Text="Button" />";
我在C#代码中写入了此代码,但出现设计时错误.谁能建议我我做错了什么?实际上,我是通过c#创建一个按钮.
I am wrting this code in my c# code, but I am getting design time error. Can anyone suggest me where I am doing wrong? Actually I am creating a button by c#.
推荐答案
在字符串文字中,有两种转义引号的方式:
就像在其他答案中一样:
There are two ways of escaping quote in a a string literal:
As in other Answers:
string target = "word in \"quotes\"";
中的单词
或者,也可以使用冗长的语法:
Or, alternatively the verbose syntax:
string target = @"word in ""quotes""";
同样,冗长的语法还用于解释多行字符串的源代码行,其原义为行尾:
Also, the verbose syntax is designed to interpret source code lines of a multi-line strings with end-of-lines in the literal:
string target = @"A multi-line
string,
verbosely";
虽然其他答案在技术上是正确的,但有关如何对字符串中的引号进行转义,但这无助于您在C#中动态创建按钮.以下是如何完成此操作的示例:
While the other answers are technically correct about how you escape quotes in a string, that will not help you dynamically create a button in C#. Here is an example of how to accomplish that:
protected void Page_Load(object sender, EventArgs e)
{
Button b = new Button { ID = "btnGenerated", Text = "Click Me!" };
b.Click += new EventHandler(delegate(object mySender, EventArgs myArgs)
{
b.Text = "You Clicked Me!";
});
Page.Form.Controls.Add(b);
}
按如下所示更改您的字符串:
Change your string as follows:
string customButtonControl = "<asp:Button ID=\"Button1\" runat=\"server\" Text=\"Button\"/>";
希望这个回答您的问题
Hope this answer your question
这篇关于在C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文