How can I print a text for instance programming language without double quotes and apostrophes in source code? I made in c++ :

#include <iostream>
#define Rep(x) #x
int main()
{
   printf(Rep(programming language));
   return 0;
}

Program is ok but I wonder if there is another way to do this? and how do it in java? is any possibility do it in java?

有帮助吗?

解决方案

Insert code units as integer literals:

#include <iostream>

int main() {
    char[] string = { 0x41, 0x42, 0x43, 0x0A, 0x00 };
    std::cout << string;
}

In Java this should do:

byte[] bytes = {(byte)0x41, (byte)0x42, (byte)0x43, (byte)0x0A};
String string = new String(bytes, "UTF-8");

其他提示

The best way to avoid placing string literals in your code is to use a file to read them from. Note: you should consider clarity when looking at alternatives. Ask yourself how is

Rep(programming language)

clearer/betters than

"programming language"

BTW: if you want to make your code unreadable, you can do this in Java

String s = \u0022programming language\u0022
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top