Wednesday 8 January 2014

Java: Replace given pattern with 'X'

Replace all occurrence of the given pattern to 'X'.For example, given that the pattern = 'abc', replace 'abcdeffdfegabcabc' with 'XdeffdfegX'. Note that multiple occurrences of abc's that are contiguous will be replaced with only one 'X'.

public class HelloWorld{

     public static void main(String []args){
         String s1="Helloabckiranabchowareabcyou";
    s1=s1.trim().replaceAll("abc", "X");
    System.out.println(""+s1);
     }

}

No comments:

Post a Comment