Results 1 to 3 of 3
  1. #1
    skania is offline Member
    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0

    Default Split a string, can't escape '\'

    Basically I'm making a very basic game, playing with java mainly.. I made a save file that the characters name is used in, and the data is stored inside. I got the list of files and converted them to a string with this:
    Java Code:
    	File cfile = new File("lib");
    			File filematches[] = cfile.listFiles(new FilenameFilter()
    					{ public boolean accept(File dir, String name){
    						return name.endsWith(".save");
    					  }
    					});
    			for(int i = 0; i < filematches.length; i++){
    				savefiles[i] = filematches[i].toString();
    			}
    Then I parse through savefiles[], where I want to extract the index 1.
    example file name returned: lib\char.save

    I've tried without success to escape the \ so that I can delimit the string at \ and .
    Here is what I have been using:
    Java Code:
    			for(String el : savefiles){
    				if(el != null){
    					String delimeter = "[.\\]";
    					String[] tokens = el.split(delimeter);
    					System.out.println(tokens[0]);
    				}
    			}
    But I get the error: Unexpected internal error near index 2
    .\
    I looked, and confirmed that \\ should have escaped the special character, but it seems it doesn't. Am I completely missing something? Thanks in advance for any input :)

    /edit: I have tried ".\\" "[.\\]" "[\\.]" "\\." and still have no idea why it won't work correctly.

  2. #2
    skania is offline Member
    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0

    Default Re: Split a string, can't escape '\'

    Just found the answer:

    For future seekers: you have to use "\\\\". One set of '\\' to escape the escape character that escapes the last '\'
    So:
    '\\' escapes '\' that escapes '\'.
    '\\\\'

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: Split a string, can't escape '\'

    This is what you're looking for :"[.\\\\]";

    From the Pattern API: Backslashes within string literals in Java source code are interpreted as required by The Java™ Language Specification as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6) It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how to split the value/string
    By vasu.sln in forum New To Java
    Replies: 3
    Last Post: 03-15-2013, 10:49 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •