1

I have a string in which : occurs several times. I want each occurance of : to be surrounded by double quotes, like ":" without truncating the data around it.

I have tried to use String.replaceAll(":", "\":\""); but it truncates the characters around :.

How can I enclose all occurances of : by " without truncating the characters around :?


EDIT:-

My String:-

{key1:value1,key2:value2,key3:{key4:value4,key5:value5,key6:{key7:{key8:value8}}},key9:value9}

More readable Form:-

{
    key1:value1,
    key2:value2,
    key3:{
        key4:value4,
        key5:value5,
        key6:{
            key7:{
                key8:value8
            }

        }

    },
    key9:value9
}

What I am doing:-

responseString = responseString.replaceAll(":", "\":");// Put " before each occurance of :

responseString = responseString.replace(":[^\\Q{\\E]", ":\""); // Put " after each occurence of : except the ones preceded by {

Output:-

{key1":"alue1,key2":"alue2,key3":{key4":"alue4,key5":"alue5,key6":{key7":{key8":"alue8}}},key9":"alue9}
Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    `but it truncates the characters around : ` What does that mean? Any Example? – akash Jun 18 '14 at 05:01
  • nothing wrong in calling replaceAll , are you calling it on String class , its not a static method of string class , call it on some other string – Hussain Akhtar Wahid 'Ghouri' Jun 18 '14 at 05:03
  • @TAsk `key1":value1` after doing `replaceAll(":",":/"")` becomes `key1":"alue1` – Solace Jun 18 '14 at 05:03
  • @HussainAkhtarWahid'Ghouri' Yeah I am using the particular String instance (my string) to call `replaceAll()` by dot notation like `myStrng.replaceAll(regex, replacement);` – Solace Jun 18 '14 at 05:05
  • `replaceAll(":",":/"")` is not the same as `replaceAll(":", "\":\"");` – Scary Wombat Jun 18 '14 at 05:07
  • you can try using replace method instead of replaceAll. That'll also works fine – Anoop George Jun 18 '14 at 05:07
  • 5
    [status-norepro] [could not reproduce.](http://ideone.com/Bg8eGc) – Justin Jun 18 '14 at 05:11
  • Whoever is downvoting the question and the answers, do you mind saying the reason? Because when you don't tell the reason, and we can't find out whatever the issue is, it only comes out that YOU have some issues! – Solace Jun 18 '14 at 05:23
  • 2
    not the DV'er but your original question does not reflect what it has evolved into. Hence the problem could not be reproduced and the overall answers are just being stabs-in-the-dark – Scary Wombat Jun 18 '14 at 05:35
  • 1
    BTW, would it no be better to have your json correctly produced rather than trying to hack it? – Scary Wombat Jun 18 '14 at 05:39
  • @ScaryWombat You just hit a nerve. It was some response from a `web-service` (You can see an example [here](http://stackoverflow.com/questions/24268924/weird-soap-response-is-it-json-how-to-parse-it)) which I have to parse and I am doing it this way (converting it to a format which can be converted to `JSONObject` and then retrieving values from the `JSONObject`. – Solace Jun 18 '14 at 06:29
  • @ScaryWombat By the way since you pointed out something related, may be you can suggest something about it. I had put a question on it yesterday: [http://stackoverflow.com/questions/24268924/weird-soap-response-is-it-json-how-to-parse-it](http://stackoverflow.com/questions/24268924/weird-soap-response-is-it-json-how-to-parse-it) – Solace Jun 18 '14 at 06:38

2 Answers2

0

Your method just works fine as replaceAll takes first argument as regExp second as replacement String.

For Example:

System.out.println("abc : pqr".replaceAll(":", "\":\""));

OUTPUT

abc ":" pqr

replaceAll doesn't change actual String it just returns the new replaced String

you need to assign it.

String str="key1\":value1";
str=str.replaceAll(":", "\":\"");

You can try this easy trick.

String str="{key1:value1,key2:value2,key3:{key4:value4,key5:value5,key6:{key7:{key8:value8}}},key9:value9}";
str=str.replaceAll("(:\\{)", "-");//Replace :{ to -
str=str.replaceAll("[:]", "\":\"");//replace : to ""
str=str.replaceAll("-", ":{");//again replace - to :{
System.out.println(str);
akash
  • 22,664
  • 11
  • 59
  • 87
-1

I think you are not calling it on the right string object, but rather you are calling it on String class which will result in error as replaceAll() is not a static method of String class

This works just fine for me

String str1,str2;
str1 = "Hussain:Akhtar:Wahid:Ghouri";
str2 = str1.replaceAll(":", "\":\"");
System.out.println(str1);
System.out.println(str2);

Output

Hussain:Akhtar:Wahid:Ghouri

Hussain":"Akhtar":"Wahid":"Ghouri

Community
  • 1
  • 1
  • I do not know why somebody downvoted. Although could not get my question resolved but I probably could not make it clear while trying to make it simple, that's my bad. But I just edited my question and going to atleast upvote your answer. – Solace Jun 18 '14 at 05:15
  • @Zarah : thanx for the upvote , will review your question and my ans , but I still am waiting for downvoters response , can be something to learn for me – Hussain Akhtar Wahid 'Ghouri' Jun 18 '14 at 06:00
  • 1. It's possible to have a variable called `String`, so `String.replaceAll(...)` could in fact compile and run. 2. It's entirely possible `String.replaceAll(":", "\":\"");` was an example, and `String` wasn't actually the name of the variable. 3. The program evidently compiles and runs, otherwise OP would not be able to find out that characters are being deleted. – awksp Jun 18 '14 at 06:54
  • @user3580294 : 1 :its possible to declare a variable named 'String' but not expected thus i got to another approach , 2 : I too was saying may she has called on the wrong object , 3 : I myself was telling it works absolutely fine without any error and OP – Hussain Akhtar Wahid 'Ghouri' Jun 18 '14 at 10:33
  • 1
    My point is that your guess is completely invalid. If OP tried to call `replaceAll()` as a static method the question would be *significantly* different as the code would never run, so OP would never find out that characters are being deleted. – awksp Jun 18 '14 at 10:36
  • @user3580294 : I gave both the scenario , anyways ,I wanted to know if I am missing any point , I got my ans , downvote is note the reason for me – Hussain Akhtar Wahid 'Ghouri' Jun 18 '14 at 10:56
  • You gave a wrong answer, and if you remove that what remains really should be a comment and not an answer. The point you're missing is that one of the scenarios you describe is entirely impossible. Bad answers get downvoted. – awksp Jun 18 '14 at 11:04
  • @user3580294 : downvote was hardly my point , the reason behind it was – Hussain Akhtar Wahid 'Ghouri' Jun 18 '14 at 11:44