README.md

December 5, 2022 ยท View on GitHub

_String Replace (for real)

Description

Replaces all occurances of a given substring with another substring in a String object.

I looked for a good string replacer and all of them broke (replaced things multiple times, infinitely looped, etc), so I made one. Please let me know if this doesn't work, because if it doesn't work for you then it doesn't work for me! Thanks.

More Info

aSearch=The String to search

aFind=The String to find

aReplace=The String to replace aFind with

Returns aSearch with all occurances of aFind replaced with aReplace

Submitted On
ByAtul Brad Buono
LevelBeginner
User Rating4.5 (27 globes from 6 users)
CompatibilityJava (JDK 1.2)
CategoryString Manipulation
WorldJava
Archive File

Source Code

public static String replaceString(String aSearch, String aFind, String aReplace)
{
 String result = aSearch;
 if (result != null && result.length() > 0)
 {
  int a = 0;
  int b = 0;
  while (true)
  {
   a = result.indexOf(aFind, b);
   if (a != -1)
   {
    result = result.substring(0, a) + aReplace + result.substring(a + aFind.length());
    b = a + aReplace.length();
   }
   else
    break;
  }
 }
 return result;
}