Unable to replace String occurence in java? -


I have a stringbuilder object in Java that stores some Jason formatted data created on the fly by using the Java program. I am trying to do this, trying to change the occurrence of string one with string two in stringbuilder object. I wrote the code below to change but it will not work? Any reason or effective method to replace a string? stringbuilder json = new stringbilder (); Public Zero Method 1 () {string replacelater = "{\" name \ ": \" & lt; Font color = \\\ "# ff 20000 \\\" & gt; + "+ \ funcvp_name4.trim () +" <\\ / font> \ ", \" child \ ": ["; callingmethod2 (); replacewith = "{\" name \ "with string: "& Lt; font color = \\\" # FF0000 \\\ "& gt; + + funcvp_name4.trim () + totalempforfuncvp +" & lt; \\ / font & gt; \", \"children\":[ "; String jsonnew = json.toString (); Jsonnew.replaceFirst (replacelater, replacewith); Json.setLength (-1); Json.append (jsonnew); }

Error:

  Exception in thread "main" java.util.regex.PatternSyntaxException: illegal repetition near index 2 {"name": " & lt; font color = \ "# FF20000 \" & gt; (7h Coast 806)  "," Kids ": [^ at java.util.regex.Pattern.error ( Unknown source) at java.util java.util.regex.Pattern.com at java.util.regex.Pattern.expr (unknown source) at java.util.regex.Pattern.sequence (unknown source) regex.Pattern.closure (unknown source) (unknown source) ) At java.util.regex.Pattern & lt; Init & gt; (Unknown source) at java.util.regex.Pattern.compile (unknown source) java.lang.String.replaceFirst (unknown source) CreateChart.iterateFuncVPNamesFromArrayList (CreateChart .java: 496) at CreateChart.getDataFromEMPHCForCVp (CreateChart.java:110 ) CreateChart.main on (CreateChart.java:964)    

Two code in your code Problems are first of all, the logic of the first replaceFirst is considered as a regular expression, so things like backslabs and braces may play literal things to find meta characters. Y - replaceFirst to you pattern.quote .

need to be used. The strings in the other are irreversible - replaceFirst does not modify the string it was called, it returns the result as a new string.

  string jsonnew = json.toString (). Change Fast (Pattern Quat (replacelater), replacewith);   

Alternatively, the replaced method treats its first parameter as a literal string rather than a rajx but it all Will change the events, not just the first time.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -