character literals and string

Discussion in 'Java' started by Pete Elmgreen, Nov 24, 2004.

  1. 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
     
    Pete Elmgreen, Nov 24, 2004
    #1
    1. Advertising

  2. Pete Elmgreen

    VisionSet Guest

    "Pete Elmgreen" <> wrote in message
    news:...
    > 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 \


    --
    Mike W
     
    VisionSet, Nov 24, 2004
    #2
    1. Advertising

  3. Pete Elmgreen wrote:
    > 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.
     
    Michael Borgwardt, Nov 24, 2004
    #3
  4. Pete Elmgreen

    Ann Guest

    "Pete Elmgreen" <> wrote in message
    news:...
    > 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';
     
    Ann, Nov 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    8
    Views:
    16,468
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    742
    those who know me have no need of my name
    May 28, 2006
  3. Lawrence D'Oliveiro
    Replies:
    76
    Views:
    1,643
    Arne Vajhøj
    Feb 27, 2011
  4. Lauri Alanko

    Encoding of character literals

    Lauri Alanko, Nov 3, 2011, in forum: C Programming
    Replies:
    4
    Views:
    302
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    744
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page