C# 字符串连接和@
我有以下使用如下字符串的代码:C# 字符串连接和@,c#,string,C#,String,我有以下使用如下字符串的代码: string str = @"this is very important string ,which uses hardcoded name"; 我想将其更改为具有输入参数的值 public void func (string name) { // some code string str = "this is very important string ,which uses " + name; } 当我生成字符串时,我仍然需要使用“@”,
string str = @"this is very important string ,which uses hardcoded name";
我想将其更改为具有输入参数的值
public void func (string name)
{
// some code
string str = "this is very important string ,which uses " + name;
}
当我生成字符串时,我仍然需要使用“@”,我能做什么
string str = @("this is very important string ,which uses " + name);
如果要逐字处理转义序列,可以在每个字符串文本之前使用
@
(并且仅限文本,而非变量)。因此,在您的情况下,您只需执行以下操作:
string str = @"this is very important string ,which uses " + name;
顺便说一句,对使用@
这是一个非常重要的字符串,它使用“
不会有什么区别,因为它没有任何转义序列
更多关于:
逐字字符串文字由@字符后跟双引号字符、零个或多个字符以及结束双引号字符组成。一个简单的例子是@“你好”。在逐字字符串文字中,分隔符之间的字符将逐字解释,唯一的例外是引号转义序列。特别是,简单转义序列以及十六进制和Unicode转义序列不以逐字字符串文字处理。逐字字符串文字可以跨越多行
因此,@
起作用的示例可能是制表符\t
:
string c = "hello \t world"; // hello world <-- tab here
string d = @"hello \t world"; // hello \t world <-- treated literally
string c=“hello\t world”//你好,世界,你说的“我仍然需要使用”@“是什么意思?您得到了什么错误?string str=string.Format(“这是非常重要的字符串,它使用{0}”,名称);是格式化字符串的更好选项