Search code examples
javasubstr

How to Split the String into sub string in java


I am getting this data from my local server.

Bundle[{json={"productId":"4","unseenIds":[1,4,8]",
   "id":"8","message":"You have a new request for your product"}, 
   collapse_key=do_not_collapse}]

I have to split this data into:

{"productId":"4","unseenIds":"[1,4,8]",
   "id":"8","message":"You have a new request for your product"}

How can I do this ?


Solution

  • Split with = and remove extra things, This would be a dirty solution, since your String is not properly formatted, I am not finding anything good.

    suppose str is your string,

      String tokens[] =  str.split("=");
      String result = tokens[1].replace(", collapse_key","");
    

    or

      String result = tokens[1].substring(0, (tokens[1].length()  -15));
    

    Another solution would be like this,

       result = str.substring(<index of first = >, str.length() - <length of unwanted string at the end> );
    

    ie,

     //re-check indexes  
     result = str.substring(13, str.length()-35);