Sure, here’s the translation: “如何在Java中进行不区分大小写的字符串拆分?”

huangapple go评论80阅读模式
英文:

How to split the String without case-sensitive in java?

问题

如何在Java中不区分大小写地拆分字符串?

String sql = SELECT * FROM employee OFFSET 0 LIMIT 25;
String[] sqlWithoutPagination = sql.split("offset", -1);

OFFSET 可以是小写或大写。

英文:

How to split the String without case-sensitive in java?

String sql = SELECT * FROM employee OFFSET 0 LIMIT 25;
String[] sqlWithoutPagination = sql.split("offset");

OFFSET can be in lower-case/upper-case.

答案1

得分: 2

使用正则表达式的话,如下所示:

    String sql = "SELECT * FROM employee OFFSET 0 LIMIT 25";
    System.out.println(sql.split("(?i)OFFSET")[0]);
英文:

using regex would be like below

String sql = "SELECT * FROM employee OFFset 0 LIMIT 25";
System.out.println(sql.split("(?i)offset")[0]);

答案2

得分: 1

你可以不使用正则表达式,只需使用indexOftoLowerCase

String sql = "SELECT * FROM employee OFFSET 0 LIMIT 25";
int idx = sql.toLowerCase().indexOf("offset");
String sqlWithoutPagination = sql.substring(0, idx);

当然,你需要检查idx是否大于等于0。

英文:

You don't have to use a RegEx, you can simply use indexOf and toLowerCase:

String sql = "SELECT * FROM employee OFFSET 0 LIMIT 25";
int idx = sql.toLowerCase().indexOf("offset");
String sqlWithoutPagination = sql.substring(0, idx);

Of course, you would have to check if idx is greater equals 0.

答案3

得分: 1

我实际上建议在这里使用String#replaceAll,以不区分大小写的方式,仅在出现时删除OFFSETLIMIT子句:

String sql1 = "SELECT * FROM employee OFFSET 0 LIMIT 25";
String sql1New = sql1.replaceAll("(?i)(?:\\s+OFFSET \\d+)?\\s+LIMIT \\d+$", "");
System.out.println(sql1 + "\n" + sql1New);
String sql2 = "SELECT * FROM employee LIMIT 25";
String sql2New = sql2.replaceAll("(?i)(?:\\s+OFFSET \\d+)?\\s+LIMIT \\d+$", "");
System.out.println(sql2 + "\n" + sql2New);

这将打印:

SELECT * FROM employee OFFSET 0 LIMIT 25
SELECT * FROM employee
SELECT * FROM employee LIMIT 25
SELECT * FROM employee
英文:

I would actually suggest using String#replaceAll here in case insensitive mode, to just strip off the OFFSET and LIMIT clauses, should they occur:

String sql1 = "SELECT * FROM employee OFFSET 0 LIMIT 25";
String sql1New = sql1.replaceAll("(?i)(?:\\s+OFFSET \\d+)?\\s+LIMIT \\d+$", "");
System.out.println(sql1 + "\n" + sql1New);
String sql2 = "SELECT * FROM employee LIMIT 25";
String sql2New = sql2.replaceAll("(?i)(?:\\s+OFFSET \\d+)?\\s+LIMIT \\d+$", "");
System.out.println(sql2 + "\n" + sql2New);

This prints:

SELECT * FROM employee OFFSET 0 LIMIT 25
SELECT * FROM employee
SELECT * FROM employee LIMIT 25
SELECT * FROM employee

huangapple
  • 本文由 发表于 2020年8月12日 15:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63372204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定