2

I am getting http response as an array from php in android. The response i sent is a ULR. When i get it, it comes as:

["http:\/\/karan.development.com\/Image_android\/upload_image_14552.jpg"]

I tried to remove the charachters as:

String trim;
trim = beforeTrim.replaceAll("[\]", "");

here, "beforeTrim" is a string where the url is stored. But unfortunately it dosen't works. Please anyone help

Abstract
  • 561
  • 1
  • 8
  • 29
  • 1
    How are you receiving your input? The string you've supplied appears to be JSON-encoded. If you deserialize your response using GSON or some other JSON deserializer, I would expect the escape characters to disappear. – Simon MᶜKenzie Feb 19 '15 at 03:07

2 Answers2

3

I suspect you just need to escape the regex escape sequence (which is \):

trim = beforeTrim.replaceAll("\\\\", "");
System.out.println(trim);

that prints:

http://karan.development.com/Image_android/upload_image_14552.jpg

The reason for 4 \ characters is:

  • regex needs two \\ to escape \ and make it a \ character
  • java String literals need two \\ to escape \ and make it a \ character
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
1

Hy karan, you just have to do this for special characters:

String trim;
String trim = beforeTrim.replaceAll("[|?*<\">+\\[\\]/']", "");
System.out.println("After trimming"+" " +trim);

The result you will get is:

http://karan.development.com/Image_android/upload_image_14552.jpg

Hope this will help !

Christine
  • 514
  • 1
  • 5
  • 14