Results 1 to 2 of 2
  1. #1
    cecily is offline Member
    Join Date
    Jul 2007
    Posts
    33
    Rep Power
    0

    Default problems with replace method

    I have this string:
    Java Code:
    String url="C:\file\test.txt";
    and I want to write '/' instead of '\'
    like this
    Java Code:
    url="C:/file/test.txt";
    I've tried to use the replace but It didn't work

    Java Code:
    String url="C:\file\test.txt";
    System.out.println(url.replace('\\','/'));
    please help me

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Backslashes in java are escape tokens. To use them as character literals they need to be preceded by a backslash, ie, escaped. So if you want '\' to appear you have to write it as '\\'.
    Java Code:
    String escapedLetters = "C:\file\test.txt";
    System.out.println("escapedLetters = " + escapedLetters);
    String url = "C:\\file\\test.txt";
    System.out.println("url = " + url);
    String converted = url.replace('\\','/');
    System.out.println("converted = " + converted);

Similar Threads

  1. Using a replace method...
    By paul in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 04:50 AM
  2. problems to find the main method
    By christina in forum Eclipse
    Replies: 2
    Last Post: 08-06-2007, 07:51 PM
  3. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM
  4. Problems with method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:49 PM
  5. Problems with Find method in EJB
    By Nick15 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 05-14-2007, 01:29 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
  •