I'm trying to implement some code that downloads files from some webpages.

I am using the built-in functions Import and StringReplace.

I found that when I import a url that contain blanks I need to replace them with %20, however in some strings "\" appears suddenly.

For example,

StringReplace[http://ww.aaa.com/asd/dfh asda asd.pdf, " " -> "%20"]

I expect this to give "http://www.aaa.com/asd/dfh%20asda%20asd.pdf".

Using this string with FetchURL, I try to download the file but it does not work.

I found that when I copy the same string and paste it somewhere, it looks like:


I suspect that the "\" is causing the problem. What's the proper way to deal with "\" characters here?

  • 1
    $\begingroup$ StringReplace["hptt://ww.aaa\.com/asd/dfh asda asd.pdf", {" "->"%20", "\\"->""}] returns "hptt://ww.aaa.com/asd/dfh%20asda%20asd.pdf" Be sure to use two backslash in the pattern. $\endgroup$
    – Bill
    Sep 3 '15 at 17:25
  • 6
    $\begingroup$ A better approach is to use URLEncode["yourstring"] (docs here) that will take care of all the URL encoding for you. $\endgroup$
    – MarcoB
    Sep 3 '15 at 17:40
  • $\begingroup$ I would guess there is something odd/non-ascii in your original string. $\endgroup$
    – george2079
    Sep 3 '15 at 18:22
  • $\begingroup$ @Bill As you said I tried that but does not work. However, recently I found that when I copied my string with "Plain Text Copy" , it worked perpectly. Can you see what is the problems?? $\endgroup$
    – L. JIN
    Sep 6 '15 at 6:22
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Oct 3 '15 at 3:07


URLSave @ "http://geniusquotes.org/wp-content/uploads/2014/06/The-quieter-you-become.jpg"




Web Operations


You might try two possibilities (or both) and let me know if the problem persists:

arg = "your string";

str1 = StringReplace[ToString[arg], "\[Continuation] " -> ""];

str2 = ImportString[ExportString[str1, "Text"], "Text"];

Do str1 or str2 still contain the symbol you see?

  • $\begingroup$ Also I tried your way but result was negative. As you can see comment to bill, I found that when I copied my string with "Plain Text Copy" , it worked perpectly. Can you see the problem? $\endgroup$
    – L. JIN
    Sep 6 '15 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.