case sensitive and insensitive condition in String replace function
String target = "FooBar";
target = target.replaceAll("foo", "");
System.out.println(target);
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:
Post a Comment