Hi friends I'm creating an app.
I want to find a particular word in an ArrayList
and I have to replace
it with another word. I used the code below. It works case sensitive,
but I'd like to get it working case insensitive.
FillintheBlank.class:
public class FillintheBlank extends Activity {
static ArrayList<String> multiword=new ArrayList<String>();
static ArrayList<String> multimeaning=new ArrayList<String>();
public void setNoTitle() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
float screenHeight,screenWidth,screendensity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setNoTitle();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
setContentView(R.layout.fillinblanknew);
multiword.add("radha");
multiword.add("RAdHA");
multiword.add("latha");
multiword.add("mammu");
s.o.p(""+multiword);
// output:radha,RADHA,latha,mamu
multimeaning.add(multiword.getString().replace(radha,"sai"));
s.o.p(""+multimeaning);
// output: sai,RADHA,latha,mamu
}
}
For example: I need to replace 'radha' with 'sai' no matter what the case of the letters in 'radha' are.
Below is the python program to do case insensitive string replacement : import re given_text = 'Hello ! hello All ! HELLO everyone !' new_text = re.sub('hello', 'Hi', given_text, flags=re.IGNORECASE) print(new_text)
The option to replace case insensitive regex in java is Pattern.CASE_INSENSITIVE, which can also be specified as (?i) (http://boards.developerforce.com/t5/Apex-Code-Development/Why-Pattern-CASE-INSENSITIVE-not-detectable/td-p/204337)
Explanation : BeSt is replaced by “better” ignoring cases. In this, sub () of regex is used to perform task of replacement and IGNORECASE, ignores the cases and performs case-insensitive replace. Using particular ignore case regex also this problem can be solved.
You can " (?i)" pattern in front of a string to ignore the case. public class StringReplace { public static void main (String [] args) { System.out.println (replaceString ("This is a FISH", "IS")); } public static String replaceString (String first, String second) { return first.replaceAll (" (?i)"+ second, ""); } }
Could use a regular expression. Just add (?i) before your string to ignore case.
So for example:
multiword.getString().replaceAll ( "(?i)radha", "sai");
You can "(?i)" pattern in front of a string to ignore the case.
public class StringReplace {
public static void main(String[] args) {
System.out.println(replaceString("This is a FISH", "IS"));
}
public static String replaceString(String first, String second) {
return first.replaceAll("(?i)"+ second, "");
}
}
The code above will return "Th a FH".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With