character literals and string

P

Pete Elmgreen

Hi all,

I've got a newbie question - I tried different things but can't get the syntax right
This is what need to do: concatenate character literals (hex value) and a string

String s;
s = '\x0b' + "my string" + '\x1C' + '\x0D' ;

Compiler says illegal token "\" will be ignored (twice)

How can I fix this

TIA
Pete
 
V

VisionSet

Pete Elmgreen said:
Hi all,

I've got a newbie question - I tried different things but can't get the syntax right
This is what need to do: concatenate character literals (hex value) and a string

String s;
s = '\x0b' + "my string" + '\x1C' + '\x0D' ;

Compiler says illegal token "\" will be ignored (twice)

How can I fix this

String s = Integer.toHexString(0xFF) + " myString ";

\x is not a valid escape character
'?' where ? is a single valid character
'\u0123' is valid unicode character since 0x0123 is valid hex value for a
unicode character
valid escape characters are
\f formfeed
\n newline
\r carriage return\b backspace
\t tab
\u unicode hex value follows
\' literal '
\" literal "
\\ literal \
 
M

Michael Borgwardt

Pete said:
I've got a newbie question - I tried different things but can't get the syntax right
This is what need to do: concatenate character literals (hex value) and a string

String s;
s = '\x0b' + "my string" + '\x1C' + '\x0D' ;

Compiler says illegal token "\" will be ignored (twice)

Character literals can be specified in four ways:

'a' (actual character literal)
'\141' (octal numerical escape)
'\u0061' (unicode escape)
'\n' (special escape sequence)

Note that unicode escapes are processed before the code is parsed, so
they should not be used for control characters (especially linefeed
and carriage return).

Also, character literals are of type char and thus are integer types.
Attempting to concatenate two chars with + will result in their
numerical values to be added.

Better use a StringBuffer in such cases.
 
A

Ann

Pete Elmgreen said:
Hi all,

I've got a newbie question - I tried different things but can't get the syntax right
This is what need to do: concatenate character literals (hex value) and a string

String s;
s = '\x0b' + "my string" + '\x1C' + '\x0D' ;

Compiler says illegal token "\" will be ignored (twice)
This compiles: (octal notation)
String s = '\013' + "my string" + '\034' + '\015';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top