Skip to content

C# equivalent for appendReplacement in java

Jans

Asked Feb 11, 2007, 11:53 PM EST1 answer
Share
LinkedInX
Is there any C# equivalent code for the appendReplacement  functionality in JAVA. I guess no! but could anyone help me to get it?

Java Code:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
     m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());

O/P: one dog two dogs in the yard

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Well, in that specific example, you could do something like so:
    JavaScript
    
    String x = new String( "The cat likes to eat hotdogs." );
    x.Replace( "cat", "dog" );
    Not sure if that's what you need, but otherwise you would indeed need to write a little method to do it.
    0