Search code examples
javajava-6

String Manipulation in java 1.6


String can be like below. Using java1.6

String example = "<number>;<name-value>;<name-value>";

String abc = "+17005554141;qwq=1234;ddd=ewew;otg=383";
String abc = "+17005554141;qwq=123454";
String abc = "+17005554141";

I want to remove qwq=1234 if present from String. qwq is fixed and its value can VARY like for ex 1234 or 12345 etc

expected result :

String abc = "+17005554141;ddd=ewew;otg=383";
String abc = "+17005554141"; \\removed ;qwq=123454
String abc = "+17005554141";

I tried through

abc = abc.replaceAll(";qwq=.*;", ""); 

but not working.


Solution

  • I came up with this qwq=\d*\;? and it works. It matches for 0 or more decimals after qwq=. It also has an optional parameter ; since your example seems to include that this is not always appended after the number.

    I know the question is not about javascript, but here's an example where you can see the regex working:

    const regex = /qwq=\d*\;?/g;
    var items = ["+17005554141;qwq=123454",
                "+17005554141",
                "+17005554141;qwq=1234;ddd=ewew;otg=383"];
    
    for(let i = 0; i < items.length; i++) {
      console.log("Item before replace: " + items[i]);
      console.log("Item after replace: " + items[i].replace(regex, "") + "\n\n");
    }