7

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.

il_guru
  • 8,383
  • 2
  • 42
  • 51
radha
  • 192
  • 1
  • 2
  • 13

4 Answers4

12

Could use a regular expression. Just add (?i) before your string to ignore case.

So for example:

multiword.getString().replaceAll ( "(?i)radha", "sai");

Alex Florescu
  • 5,096
  • 1
  • 28
  • 49
1

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".

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
rslj
  • 330
  • 1
  • 3
  • 13
0

Use StringUtils by apache commons lang3 - v3.5 or later (so you don't have to worry about regular expression parsing):

StringUtils.replaceIgnoreCase(text, searchString, replacement);

or:

StringUtils.replaceOnceIgnoreCase(text, searchString, replacement);

If you use maven, add this dependency (I'm using version 3.6):

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>
Naor Bar
  • 1,991
  • 20
  • 17
-4

You could use equalsIgnoreCase(string) method found in String class for your purpose.

Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
  • yes i tried.please see my code once. first i need to search in arraylist that string is there or then i have to apply that equlasignore case() butin above code how to apply that method i didnt get any idea – radha Mar 25 '13 at 11:05
  • 1
    @TimothyP OP wants insensitive case replace. Not equals. If I am wrong in downvoting, I will undo it. But I think the answer is not proper. – MysticMagicϡ Mar 25 '13 at 11:09
  • Well, his answer may have been better as a comment now that I think of it. You're correct – TimothyP Mar 25 '13 at 11:11