java 如何进行不区分大小写的字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15613209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:16:05  来源:igfitidea点击:

How to do a case-insensitive string replacement

javaandroidandroid-emulator

提问by radha

Hi friends I'm creating an app.
I want to find a particular word in an ArrayListand 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.

嗨,朋友们,我正在创建一个应用程序。
我想在 an 中找到一个特定的词ArrayList,我必须
用另一个词替换它。我使用了下面的代码。它区分大小写,
但我想让它不区分大小写。

   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.

例如:无论“radha”中的字母大小写如何,我都需要将“radha”替换为“sai”。

回答by Alex Florescu

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

可以使用正则表达式。只需在字符串前添加 (?i) 即可忽略大小写。

So for example:

例如:

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

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

回答by rslj

You can "(?i)" pattern in front of a string to ignore the case.

您可以在字符串前面使用“(?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, "");
    }
}

The code above will return "Th a FH".

上面的代码将返回“Th a FH”。

回答by Naor Bar

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

通过apache commons lang3 - v3.5 或更高版本使用 StringUtils (因此您不必担心正则表达式解析):

StringUtils.replaceIgnoreCase(text, searchString, replacement);

or:

或者:

StringUtils.replaceOnceIgnoreCase(text, searchString, replacement);

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

如果您使用 maven,请添加此依赖项(我使用的是 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>

回答by Rajitha Siriwardena

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

您可以将equalsIgnoreCase(string)String 类中的方法用于您的目的。