简体   繁体   English

不区分大小写的 String split() 方法

[英]Case insensitive String split() method

When I perform当我执行

String test="23x34 ";
String[] array=test.split("x"); //splitting using simple letter

I got two items in array as 23 and 34我在数组中有两个项目 23 和 34

but when I did但是当我这样做的时候

String test="23x34 ";
String[] array=test.split("X"); //splitting using capitalletter

I got one item in array 23x34我在 23x34 数组中得到了一项

So is there any way I can use the split method as case insensitive or whether there is any other method that can help?那么有什么方法可以将 split 方法用作不区分大小写的方法,或者是否有任何其他方法可以提供帮助?

split uses, as the documentation suggests, a regexp. split使用,正如文档所建议的那样,一个正则表达式。 a regexp for your example would be :您的示例的正则表达式将是:

"[xX]"

Also, the (?i) flag toggles case insensitivty.此外, (?i)标志切换不区分大小写。 Therefore, the following is also correct :因此,以下内容也是正确的:

"(?i)x"

In this case, x can be any litteral properly escaped.在这种情况下, x可以是任何正确转义的文字。

Use regex pattern [xX] in splitsplit使用正则表达式模式[xX]

String x = "24X45";
String[] res = x.split("[xX]");
System.out.println(Arrays.toString(res));

您还可以在正则表达式中使用嵌入式标志:

String[] array = test.split("(?i)x"); // splits case insensitive

I personally prefer using我个人更喜欢使用

String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
String[] parts = modified.split(splitterValue);

In this way you can ensure any regex will work, as long as you have a unique splitter value通过这种方式,您可以确保任何正则表达式都可以工作,只要您有一个唯一的拆分器值

In addition to the existing answers, you can use Pattern.CASE_INSENSITIVE flag to convert your regex pattern into a case-insensitive pattern which you can directly use to split your string eg除了现有的答案,您可以使用Pattern.CASE_INSENSITIVE标志将您的正则表达式模式转换为不区分大小写的模式,您可以直接使用它来分割字符串,例如

String[] arr = Pattern.compile("x", Pattern.CASE_INSENSITIVE).split("23x34 ");

Demo:演示:

import java.util.Arrays;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("x", Pattern.CASE_INSENSITIVE);
        
        System.out.println(Arrays.toString(pattern.split("23x34 ")));
        System.out.println(Arrays.toString(pattern.split("23X34 ")));
    }
}

Output:输出:

[23, 34 ]
[23, 34 ]

For JavaScript:对于 JavaScript:

var test="23x34 ";
var array = test.split(\x\i);

Java's String class' split method also accepts regex. Java 的 String 类的 split 方法也接受正则表达式。

To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split为了简短起见,这应该对您有所帮助: http : //www.coderanch.com/t/480781/java/java/String-split

It's a bit complex, but here's how it could be implemented:这有点复杂,但这是它的实现方式:

  1. Lowercase both the strings (overall text and search term)小写两个字符串(整体文本和搜索词)
  2. Run the text.split(searchTerm)运行text.split(searchTerm)
  3. This gives you an array of strings that are NOT search terms这为您提供了一个不是搜索词的字符串数组
  4. By walking through this array, you're getting lengths of each of these strings通过遍历这个数组,你会得到每个字符串的长度
  5. Between each of those strings, there must be a search term (with known length)在每个字符串之间,必须有一个搜索词(已知长度)
  6. By figuring out indexes, you can now .slice() the pieces from the original string通过找出索引,您现在可以 .slice() 原始字符串中的片段

You could use a regex as an argument to split , like this:您可以使用正则表达式作为split的参数,如下所示:

"32x23".split("[xX]");

Or you could use a StringTokenizer that lets you set its set of delimiters, like this:或者您可以使用StringTokenizer来设置其分隔符集,如下所示:

StringTokenizer st = new StringTokenizer("32x23","xX");
//                                          ^^    ^^
//                                       string delimiter

This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer .这样做的好处是,如果您想以编程方式构建分隔符列表,例如为分隔符列表中的每个小写字母添加其大写对应字母,您可以这样做,然后将结果传递给StringTokenizer

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

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