Tuesday, 15 January 2013

Java string.replace(old, new) count how many replaced? -


I have my console (image below), and I have a command that will replace all the oldest strains in the nuances. But how does it count how many of these places were changed?

(If the code is only converted to B once, then it will be 1, but if this value is twice the value then 2)

(This is just a part of the code, but no other part is required or related to this part of the code)

  else if (intext. StartsWith ("replace")) {string [] replist = original.split ("+"); String repfrom = replication [1]; String Rippet = Ripple [2]; Last time = repfrom; Last resort = empty; String outtext = output.gettext (); String newtext = outtext.replace (repfrom, repto); Output.setText (newtext); Int totalreplaced = 0; / / How were many replacement wires received? Message ("Total replaced:" + + + + "from" + totalreplaced + "to" + repfrom + "); }   

my console image < P>

You can use String.replace first and calculate it yourself:

  string outtext = output .getText (); String newtext = outtext; Int totalreplaced = 0; // Check whether there is anything to change (new! Text.replaceFirst (repfrom, repto) .equals (newtext)) {newtext = newtext.replaceFirst (repfrom, repto); Totalreplaced ++; } Message ("Total replaced:" + + "Repro +" to "+ totalreplaced +" to "+ Repo");    

No comments:

Post a Comment