打印带有特殊字符的字符串变量
Print a string variable with its special characters
当我想在字符串中使用特殊字符时,我应该使用"":
std::string text("item t new_itemn")
但是,如果我输出这个字符串,显然,它会输出:
item new_item
是否有办法设置std::cout来打印所有的特殊字符:
item t new_item n
t
和n
将分别打印一个制表符(4个空格)和一个新行。
如果要输出t
和n
。您必须在t
和n
之前添加一个额外的作为前缀。
std::string text("item \t new_item \n")
没有内置的方法可以做到这一点。您需要手动转义字符。例如,在C中,它应该是这样的:
for (const char* p = text; *p != ' '; ++p)
{
int c = (unsigned char) *p;
switch (c)
{
case '':
printf("\\");
break;
case 'n':
printf("\n");
break;
case 'r':
printf("\r");
break;
case 't':
printf("\t");
break;
// TODO: Add other C character escapes here. See:
// <https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences>
default:
if (isprint(c))
{
putchar(c);
}
else
{
printf("\x%X", c);
}
break;
}
}
(注意,我写上述答案时,问题仍然带有C标记,我使用printf
,因为我发现它比使用c++的iostream格式化机制更简单(特别是用于演示目的)。)
c++标准库不直接支持转义字符串。
您可以相当直接地实现它,但这会遇到三个问题:
-
对于ASCII的转义字符(代码27)有非标准的转义,例如
e
,由例如g++支持。 -
每个
char
(CHAR_BIT
来自<limits.h>
)的比特数,以及数字转义中必要的位数,与系统有关。 -
字符是否可打印,即是否需要转义,取决于语言环境。当前语言环境的竞争指示器的数量使情况变得更加复杂:C级全局语言环境,c++级全局语言环境,或者例如
cout
? 中所包含的语言环境。
通过一些硬连线的选择,实现代码看起来像这样:
#include <stdio.h> // ::sprintf
#include <ctype.h> // ::isprint
#include <string> // std::string
#include <unordered_map> // std::unordered_map
namespace my{
using std::string;
using std::unordered_map;
auto string_from( char const ch )
-> string
{ return string( 1, ch ); } // Note: "string{1,ch}" is something else.
auto is_printable( char const ch )
-> bool
{ return !!::isprint( static_cast<unsigned char>( ch ) ); }
auto escaped( char const ch )
-> string
{
static unordered_map<char, string> const escapes =
{
{ 'a', "\a" }, // 7, ^G, alert (bell)
{ 'b', "\b" }, // 8, ^H, backspace
{ 't', "\t" }, // 9, ^I, tab
{ 'n', "\n" }, // 10, ^J, newline / linefeed
{ 'v', "\v" }, // 11, ^K, vertical tab
{ 'f', "\f" }, // 12, ^L, formfeed
{ 'r', "\r" }, // 13, ^M, carriage return
{ 27, "\e" }, // 27, ^[, escape (NON-STANDARD)
{ '', "\\" } // backslash
};
auto const it = escapes.find( ch );
if( it != escapes.end() )
{
return it->second;
}
else if( is_printable( ch ) )
{
return string_from( ch );
}
else
{
int const code = static_cast<unsigned char>( ch );
char buf[] = "\xDDDD";
sprintf( buf + 2, "%04X", code );
return buf;
}
}
auto escaped( string const& s )
-> string
{
string result;
for( char const ch: s )
{
result += escaped( ch );
}
return result;
}
} // namespace my
#include <iostream>
#include <locale.h>
using namespace std;
auto main()
-> int
{
using my::escaped;
auto const text = "item t xC7x81new_itemn"s;
setlocale( LC_ALL, "" );
cout << escaped( text ) << 'n';
}
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 从字符串变量中逐字符读取单词
- 对函数 PlaySound 使用字符串变量
- C++中的字符串变量
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 我可以用字符串变量而不是它的名字创建容器吗? C++
- 字符串变量的大小
- 为什么我的字符串变量没有打印完整的字符串?
- 字符串变量未更新 c++
- 如何使用 setfill 和 setw 在字符串变量中存储十六进制值
- 在.cpp文件之间传递全局字符串变量?
- 字符串变量顺序
- 为什么可以将整数分配给字符串变量?
- 在qt创建器中调试时如何访问字符串变量的完整值?
- 将字符串变量分配给无符号字符变量
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- C++我应该用什么来保存格式化为字符串变量(如sprintf())的文件
- 我有一个字符串变量单词= "APA"
- 将字符串变量从 bash 脚本传递到根宏