' \ '-Invalid character constant?

前端 未结 7 1604
夕颜
夕颜 2020-12-19 11:27

I need to do this:

while (result2.charAt(j)!=\'\\\'){

    }

I get an error saying: Invalid character constant.

Why? a

相关标签:
7条回答
  • 2020-12-19 12:00

    Use '\\'. It's because backslash is used in escape sequence like '\n'. With a single \ the compiler have no way to know.

    0 讨论(0)
  • 2020-12-19 12:02

    The backslash is a special character and it needs to be escaped with another backslash. Like this:

    while (result2.charAt(j)!='\\'){
    
    }
    
    0 讨论(0)
  • 2020-12-19 12:06

    Same error here, but using unicode character representation.

    005C is backlash character. Need to escape it: "\u005C" .

    Example:

    str = str.replace("\\u005C", "'\\u005C'");
    
    0 讨论(0)
  • 2020-12-19 12:10

    You need to escape it I think,

    So you need to do

    while(results2.charAt(j)!='\\')
    {
    }
    

    I think that's the solution I think

    0 讨论(0)
  • 2020-12-19 12:10

    I got this similar error in Eclipse for Android although for different situation, and I just figured out that in Java you cannot enclose a string (multi-character word) in single quotes. So you need to have like - "sampleword" strings enclosed in double quotes rather than single quote to get rid of such error thought I could just share it here for others to refer..

    0 讨论(0)
  • 2020-12-19 12:16

    Looks like you need to escape the backslash. Try

    while (result2.charAt(j)!='\\'){
    
        }
    
    0 讨论(0)
提交回复
热议问题