Results 1 to 6 of 6

Thread: Error - Illegal escape character

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Error - Illegal escape character

    Hello,
    I have the following statement to me is problematic:
    Code:
    public static String str = "\.foo ";
    The compilation error is on the "\." With "." which is an "illegal escape character". In fact as I know that. " is a metacharacter is that I wish it were considered a normal character, so I put the \ in front. Do you have a solution for this problem, if yes then please post back.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Error - Illegal escape character

    Hello,
    I think the foo is going very well in a normal string. It is when you use regular expressions you must add the escape characters. It should not be any problem to understand. "Foo". In the String base, Java does not understand \ n \ t etc.. when used as characters. If you need more help on special characters then you can visit the sun's official site and their you can find detailed information on the same.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Error - Illegal escape character

    Hello,
    What the above post says is quite correct, but I wanted to add a little precision that might be be useful to you:
    If you want to make a str.split () on the "." You should submit it like this: str.split ( "\ \.") Where it becomes twisted it to make a split on the character " '\", you have to spend four: str.split ( "\ \ \ \"). Hope this solution helps you and you will find a correct solution for the same.

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Error - Illegal escape character

    Hello,
    The escape character is also giving me a problem. In fact, Java knows the string. "Foo" but what I would do is str.replaceAll (. "Foo", ""). In this case. "Foo" has been interpreted as an expression recursion.
    For example,
    if str = "t.toto" str.replaceAll (. "Foo", "") will give "t".
    But if
    str = "tt" I would absolutely not str.replaceAll (. "Foo", "") becomes "tt" too! So that's what happened. That's why I thought to use "\. Foo. But apparently the compiler does not "\." , why is it is so? Any idea about it.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Error - Illegal escape character

    Hello,
    We must double the \:
    Here is the code for it
    Code:
    str.replaceAll("\\. foo ", "");
    Why do I need to double / quadruple backslash character ( '\'). But the simplest is to use Pattern.quote () to protect all the chain:
    Here is the code for this
    Code:
    str.replaceAll(Pattern.quote(. foo "), "");
    How to replace a substring in postponing the replacement string? Now this is what is the challenging part in the code.

  6. #6
    Join Date
    Dec 2009
    Posts
    263

    Re: Error - Illegal escape character

    Hello,
    Thanks for the replies. In fact I have already tried "\ \." But as I said this string as a global variable that is used in several places, and it also broke the sudden I'm not going see below. In short, it was indeed the solution. I have used it successfully, thanks again for post a very good solution for the problem. Also if you have any alternative method then please do post back, I am the interested on here.

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2011, 08:04 PM
  2. Replies: 2
    Last Post: 08-08-2009, 10:44 AM
  3. Error : 16 bit MS-DOS subsystem NTVDM CPU illegal instruction
    By angmar67 in forum Networking & Security
    Replies: 2
    Last Post: 15-06-2009, 01:03 PM
  4. Replies: 6
    Last Post: 05-04-2007, 03:36 PM
  5. Error: RTHCDPL.EXE - Illegal System DLL relocation
    By Muhammad Waqar in forum Windows XP Support
    Replies: 3
    Last Post: 05-04-2007, 10:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,721,668,279.88817 seconds with 17 queries