Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a case-insensitive string replacement

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.

like image 998
radha Avatar asked Mar 25 '13 10:03

radha


People also ask

How to do case insensitive string replacement in Python?

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)

How to replace case insensitive regex with case insensitive in Java?

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)

How do you replace best with ignore case in regex?

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.

How to ignore the case in front of a string?

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


Video Answer


2 Answers

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

So for example:

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

like image 187
Alex Florescu Avatar answered Oct 21 '22 08:10

Alex Florescu


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

like image 28
rslj Avatar answered Oct 21 '22 09:10

rslj