0

I have to remove this.

String removeKey = "problem_keys";

Code

public class Solution {
    
public static void main(String args[]) {
    
        String removeKey = "problem_keys";
        String url = "{\"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;problem_keys:1,35,3,4,5,6,7,15,16,11,12,16;\";}";
        StringBuffer sb = new StringBuffer(url);
        removeKey(sb, removeKey);
        System.out.println(sb.toString());
    }

    public static void removeKey(StringBuffer url, String removeKey) {
        int sIndex = url.indexOf(removeKey);
        while (url.charAt(sIndex) != ';') {
            url.deleteCharAt(sIndex);
        }
        url.deleteCharAt(sIndex);
    }
}

Expected output.

{"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;}";
3
  • what is output expectation? Commented Oct 28, 2020 at 7:57
  • I want to remove problem_key: to ; in string url. url is coming dynamically not fix position and length. My code is working fine but i want to short trick of this problem. Commented Nov 5, 2020 at 12:56
  • @Er.PremSinghdaksha your code is correctly working according to your output. So, what you actually telling us to do? Commented Jan 8, 2021 at 8:28

2 Answers 2

1

Guessing you want also to remove the "values", using java 8:

 String toRemove = Arrays.stream(url.split(";")).filter(part -> part.contains(removeKey)).findFirst().orElse("");
 String newUrl = url.replace(toRemove, "");
 System.out.println(newUrl);

Speaking about the delimeters you can consider adding ";" to the toRemove string in a conditional block.

If you're aim is only to get rid of the string removeKey you can just:

url.replace(removeKey, "");
Sign up to request clarification or add additional context in comments.

3 Comments

not working your code our my expectations. I want to remove problem_key: to ; yet from my url.url is coming dynamically not fix problem_key position and length.
My code doesn't expect your url being the same length or have problem_key in the same position, it works even if there isn't a problem_key string, i compiled and executed my code before posting, I can assure you it works, and I don't see problems with different lengths or parameters positions
What my code does: It uses ";" as delimeter in order to obtain a stream of String, every element of a string is eg. ""arrival_mode:Self Drop" , ""arrival_mode:Self Drop;" ecc ... It filters the stream and find the problem key part, and then it removes that part from the initial string
0

I would go with this:

String removeKey = "problem_keys;";
url = url.replace(removeKey, "") // Replace the whole "problem_keys;" string with an empty string

1 Comment

you don't understand my problem my problem is to remove problem_keys:1,35,3,4,5,6,7,15,16,11,12,16; this is dynamic String url problem_keys value is length is not fix. Complete url come with in object String form position is not fix so anywhere problem_key: ............to ; remove this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.