[英]How do I print “\n” explicitly in a string in c++
hi i have a unknown string in c++ containing "\\n" "\\t" etc. say; 嗨,我在c ++中有一个未知字符串,包含“ \\ n”,“ \\ t”等。
string unknown1=read_from_file();
if unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n" now I want to print 如果unknown1 =“ \\ n \\ t \\ n \\ t \\ n \\ n \\ n \\ n \\ n”现在我要打印
"\n\t\n\t\n\n\n\n\n"
to the screen explcitly other than a bunch of empty spaces.... how should I do that? 到屏幕上,而不是一堆空白。...我该怎么办? remember that I don't know what is in unknown1... 请记住,我不知道未知数是什么...
to emphasize, I know that we could print \\n explicitly if we change "\\n" into "\\n" for every such character... But the problem is that I don't know what is inside unknown1... It is read from a file.... 需要强调的是,我知道,如果我们为每个这样的字符将“ \\ n”更改为“ \\ n”,则可以显式打印\\ n……但是问题是我不知道unknown1里面的内容是什么。从文件中读取...。
Thanks for answering, however we have further concerns: 感谢您的回答,但是我们还有其他担忧:
The procedure answered has one more porblem... Suppose I don't know that the only possible special character in the file is \\n or \\t... maybe there are something like \\u ?\u003c/i> 回答的过程还有一个问题...假设我不知道文件中唯一可能的特殊字符是\\ n或\\ t ...也许有类似\\ u的东西? \\l ? \\ l i think we can't exhaust every possibility right? 我认为我们不能穷尽所有可能性吧? Is there kind of built in C++ function just to output the corresponding characters? 是否有内置的C ++函数仅用于输出相应的字符?
\\n
, \\t
are escape sequences, but you can print them by adding an extra \\
before them, \\\\
is used to obtain a single backslash. \\n
, \\t
是转义序列,但是您可以通过在它们之前添加一个额外的\\
来打印它们, \\\\
用于获得单个反斜杠。 A single backslash means it is an escape sequence ( if it is a valid one ), but two backslashes represent the backslash character, so whenever you need to output a backslash, just add two of them. 单个反斜杠表示它是一个转义序列(如果它是有效的转义序列),但是两个反斜杠表示反斜杠字符,因此每当需要输出反斜杠时,只需添加两个反斜杠即可。
So, if you use 所以,如果您使用
string unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n";
You will get your desired output. 您将获得所需的输出。
If you are reading from a file , then do something like this 如果您正在读取文件,请执行以下操作
string unknown1="\n\t\n\t\n\n\n\n\n";
for ( int i = 0 ; i < unknown1.length() ; i++ )
{
if( unknown1[i] == '\n')
cout<<"\\n";
}
Like that, you will have to check for each escape sequence that may be used. 这样,您将必须检查可能使用的每个转义序列 。
Run specific checks for the non-printable characters you are worried about like this. 像这样,针对您担心的不可打印字符运行特定检查。
char c;
while(c!=EOF){
if(c=='\n')printf("\\n");
if(c=='\t')printf("\\t");
and so on and so forth.
c = the next charater;
}
oops, I wrote C instead of C++ but @Arun AS has the right syntax 糟糕,我写的是C而不是C ++,但是@Arun AS具有正确的语法
See below example. 请参见以下示例。 You can add your own characters to the switch
to extend the characters it handles. 您可以将自己的字符添加到switch
以扩展其处理的字符。
#include <iostream>
#include <string>
std::string escapeSpecialChars(const std::string& str)
{
std::string result;
for(auto c : str)
{
switch(c)
{
case '\n':
result += "\\n";
break;
case '\t':
result += "\\t";
break;
default:
result += c;
break;
}
}
return result;
}
int main()
{
std::string str = "\n\n\n\t";
std::cout << escapeSpecialChars(str);
return 0;
}
You could create your own function to print characters using a std::map: 您可以创建自己的函数以使用std :: map打印字符:
void printChar( const char ch )
{
static const std::map< char, std::string > char_map = {
{ '\n', "\\n" }
// add mappings as needed
};
auto found = char_map.find( ch );
if ( found != char_map.end() )
std::cout << found->second;
else
std::cout << ch;
}
// usage
std::string str = "a\nbc";
for ( auto ch : str ) printChar ( ch );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.