How to implement ReplaceAll in Java...
Took a look at some snippets I wrote some time ago and found the code below. Hope my blog's readers enjoy it. It is not pure .Net code but can be converted to.
package guerche.evaristo.luciano.samples
public class ReplaceAllSample
{
public static
void main(String[] args)
{
String
searchFor;
String replaceWith;
StringBuffer
searchBuffer;
searchBuffer = new StringBuffer("\tLuciano Evaristo Guerche\n\t\t\tMarcos Evaristo Guerche\n\t\t\tJuliano Evaristo Guerche");
searchFor = "Evaristo";
replaceWith = "E.";
System.out.println("Before replacing = " +
searchBuffer.toString());
while
(searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase())
> 0)
{
searchBuffer.replace(searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase()),
searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase())
+ searchFor.length(), replaceWith);
}
System.out.println("After replacing = " +
searchBuffer.toString());
searchFor = "E.";
replaceWith = "Evaristo";
System.out.println("Before replacing =" +
searchBuffer.toString());
while
(searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase())
> 0)
{
searchBuffer.replace(searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase()),
searchBuffer.toString().toUpperCase().indexOf(searchFor.toUpperCase())
+ searchFor.length(), replaceWith);
}
System.out.println("After replacing = " +
searchBuffer.toString());
searchBuffer = null;
}
}