C# 使用C在.txt文件中创建特殊字符#
在创建.txt文件时,我必须在字符串中呈现一个特殊字符。特殊字符的代码为“\x0B”。但是我们我试着把它浓缩成一个字符串,它给了我文本的原样。你能告诉我如何使用上面的代码呈现这个特殊的角色吗C# 使用C在.txt文件中创建特殊字符#,c#,console,C#,Console,在创建.txt文件时,我必须在字符串中呈现一个特殊字符。特殊字符的代码为“\x0B”。但是我们我试着把它浓缩成一个字符串,它给了我文本的原样。你能告诉我如何使用上面的代码呈现这个特殊的角色吗 String totalText = totalText + @"\x0B"+ @"Text seperated with the character"; File.WriteAllText(path + createFileName + ".txt", totalText); 您正在通过在字符串前面
String totalText = totalText + @"\x0B"+ @"Text seperated with the character";
File.WriteAllText(path + createFileName + ".txt", totalText);
您正在通过在字符串前面加“@”来使用。这意味着除了使用“
”的双引号解析为”
,没有任何内容被转义
这行吗
String totalText = totalText + "\x0B"+ @"Text seperated with the character";
File.WriteAllText(path + createFileName + ".txt", totalText);
您正在通过在字符串前面加一个“@”来使用。这意味着除了使用双引号(使用”
,解析为“
)外,不会对任何内容进行转义
这行吗
String totalText = totalText + "\x0B"+ @"Text seperated with the character";
File.WriteAllText(path + createFileName + ".txt", totalText);
删除不必要的逐字字符串文字,使用路径。组合:
string totalText = "Some text";
char c = '\x0B';
string textSeparatedWithCharacter = "Text seperated with the character";
totalText = $"{totalText}{c}{textSeparatedWithCharacter}";
// Or
//totalText = string.Format("{0}{1}{2}", totalText, c, textSeparatedWithCharacter);
File.WriteAllText(Path.Combine(path, createFileName + ".txt"), totalText);
删除不必要的逐字字符串文字,使用路径。组合:
string totalText = "Some text";
char c = '\x0B';
string textSeparatedWithCharacter = "Text seperated with the character";
totalText = $"{totalText}{c}{textSeparatedWithCharacter}";
// Or
//totalText = string.Format("{0}{1}{2}", totalText, c, textSeparatedWithCharacter);
File.WriteAllText(Path.Combine(path, createFileName + ".txt"), totalText);
帮助我们帮助您-共享您的代码完成后,添加了使用的代码
char c='\x0B'代码>帮助我们帮助您-共享您的代码完成后,添加了使用的代码char c='\x0B'
“\x0B”
应该是'\x0B'
字符串totalText=totalText+…
应该失败,因为您使用了未分配的局部变量。即使它可以编译,也没有意义,因为它里面没有值。@fubo:在字符串连接中,如果一个操作数也可以是字符串,那么将它变成字符就没有实际意义了。片段也取自问题,提问者很可能在片段周围有更多对问题不重要的代码。@Joey不是OP未能提供的吗?@fubo:»特殊字符的代码是…«(我的重点)。老实说,对于那些已经使用了一段时间类似C的编程语言的人来说,这是非常清楚和可以理解的。是的,源代码中有四个字符。不,这并不意味着这四个字符应该写入文件。您是否在抱怨源代码中的\n
是两个字符,但内存中也只有一个字符?“\x0B”
应该是”\x0B“
字符串totalText=totalText+…
应该失败,因为您使用了未分配的局部变量。即使它可以编译,也没有意义,因为它里面没有值。@fubo:在字符串连接中,如果一个操作数也可以是字符串,那么将它变成字符就没有实际意义了。片段也取自问题,提问者很可能在片段周围有更多对问题不重要的代码。@Joey不是OP未能提供的吗?@fubo:»特殊字符的代码是…«(我的重点)。老实说,对于那些已经使用了一段时间类似C的编程语言的人来说,这是非常清楚和可以理解的。是的,源代码中有四个字符。不,这并不意味着这四个字符应该写入文件。您是否在抱怨源代码中有两个字符,但内存中也只有一个?