# Manipulating strings containing backslash

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?

• 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. – Bill Sep 3 '15 at 17:25
• A better approach is to use URLEncode["yourstring"] (docs here) that will take care of all the URL encoding for you. – MarcoB Sep 3 '15 at 17:40
• I would guess there is something odd/non-ascii in your original string. – george2079 Sep 3 '15 at 18:22
• @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?? – L. JIN Sep 6 '15 at 6:22
• 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. – bbgodfrey Oct 3 '15 at 3:07

Example:

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


Reference:

URLSave

Tutorial:

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?

• 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? – L. JIN Sep 6 '15 at 6:23