英文:
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
你可以不使用正则表达式,只需使用indexOf
和toLowerCase
:
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
,以不区分大小写的方式,仅在出现时删除OFFSET
和LIMIT
子句:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论