Welcome to my blog, hope you enjoy reading
RSS

Wednesday 17 July 2013

case sensitive and insensitive condition in String replace function

case sensitive and insensitive condition in String replace function

            String target = "FooBar";
   target = target.replaceAll("foo", "");
   System.out.println(target); 
In java the above statements output is 'FooBar'
we need to replace the 'foo' string case insensitively we need to write like following.

             target = "FooBar";
   target = target.replaceAll("(?i)foo", "");
   System.out.println(target);

The output of above code is 'Bar'

Sample code:


package com.javanotes2all.java.string;

public class CaseInsesitiveReplace 
{
 public static void main(String[] args) 
 {
  //case sensitive replace
  String target = "FooBar";
     target = target.replaceAll("foo", "");
     System.out.println(target); 
     
     //case insensitive replace
  target = "FooBar";
     target = target.replaceAll("(?i)foo", "");
     System.out.println(target);
 }

}


output: FooBar Bar

0 comments: