简体   繁体   English

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

[英]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 我想在ArrayList找到一个特定的单词,我必须替换
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. 例如:无论'radha'中的字母是什么,我都需要用'sai'替换'radha'。

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”);

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”。

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>

您可以使用String类中的equalsIgnoreCase(string)方法来实现您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM