문제

So I've stumbled on what seems to be a weird little problem. I want to print control characters, which should print that weird little box suggesting that I'm trying to print a non-printable character.

When I try:

print "\^C";

it works fine. However when I do:

print (Char.toString(#"\^C"));

it literally prints "\^C". This doesn't make any sense to me, shouldn't they be printing the same thing? Char.toString doesn't convert the character to a string with one character in it, it converts it to a string with 3 characters in it, '\', '^', and 'C'.

Would somebody explain this behavior to me please? Thanks.

도움이 되었습니까?

해결책

This is because Char.toString returns a printable representation of the character.

If you wish to do a direct conversion from char to string, you need to use the str function.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top