How to make case-insensitive string replacement

Hi friends. I am creating an application.
I want to find a specific word in ArrayList, and I need to replace
it with another word. I used the code below. It works with the register,
but I would like it to be 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 happens with the letters in "radha".

+5
source share
4 answers

A regular expression may be used. Just add (? I) in front of your line to ignore case.

So for example:

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

+9
source

"(? i)" .

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, "");
    }
}

"Th a FH".

+1

Use StringUtils apache commons lang3 - v3.5 or later (so you don't need to worry about regex):

StringUtils.replaceIgnoreCase(text, searchString, replacement);

or

StringUtils.replaceOnceIgnoreCase(text, searchString, replacement);

If you are using maven, add this dependency (I am 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>
0
source

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

-3
source

All Articles