java - replace string one with instance of second string remove and replaced with third string without using replaceAll -


This is what came with me, but the last test case does not work. Any suggestions? Public class tester {public static string replacement all (string A, string b, string c) {for (;;) {int i = a.indexOf (b); If (i == - 1) {break; } A = a.sbrstring (0, i) + c + a.sbrstring (i + b.length ()); } Return; } Public static zero main (string algos []) {System.out.println (replace all ("hello o my friend, how are you?", "H", "y")); System.out.println (replace all ("CS 221 is great !!", "great", "awesome")); Println (replaceAll ("aaaa", "one", "aaa")); Let's say I'm reading my code:

  1. Find the "A" in "AAAA" Find it on the index shoe.
  2. Adds "AAAA" to start "AAAA" "aaaaaaa".
  3. "AA" in "AAAAA" finds it on index shoe.
  4. Adds "aaa" to "aaaa" yield "aaaa". ...

    So the strings will continue to grow indefinitely, to ensure that you do not explode in your strings like this, consider updating the search index from the length of the included text can do.

    In addition, if you have a lot of string containment, I recommend using a stringbinder to stop the copy of the copy.

    To say that something like this should be done:

      All public strings replaced (string A, string b, string c) {stringbilder sb. = New stringbilder (); Int i = 0; Whereas (i & lt; a.length ()) {int j = a.indexOf (b, i); If (j == -1) {sb.append (a.substring (i, a.length ())); I = a.length (); } Else {sb.append (a.substring (i, j)); sb.append (c); I = J + B Langhai (); }} Return sb.toString (); }   

    Edit - Added solution.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -