简体   繁体   English

如何删除 StringBuilder 中句子的最后一个单词?

[英]How can I delete last word of sentence in StringBuilder?

I want to detele the last word of string in StringBuilder here is it my code:我想在 StringBuilder 中删除字符串的最后一个单词,这是我的代码:

builder.append(" where");
        if(type.equals(EnumTypePrestation.OPTIQUE)){
            builder.append(" d.idDossier=p.id and d.statut=4 and  ");

        }

builder.substring(0,builder.lastIndexOf(" ")-1);

the builder contains: WHERE d.idDossier = p.id AND d.statut = 4 AND构建器包含: WHERE d.idDossier = p.id AND d.statut = 4 AND

I want to remove the last word "AND" can anyone help me to do that!我想删除最后一个单词“AND”,谁能帮我做到这一点!

Step 1 is generally to check the javadoc first, before asking on Stack Overflow.第 1 步通常是先检查 javadoc,然后再询问 Stack Overflow。 Had you done that, you would have found the StringBuilder.setLength() method which you can use to 'set the length'.如果你这样做了,你会发现StringBuilder.setLength()方法可以用来“设置长度”。 Trivially, set the length to 4 less (that same javadoc tells you that .length() gets you the current length, so, builder.setLength(builder.length() - 4) and you have your result.简单地说,将长度设置为 4 少(同样的 javadoc 告诉您.length()为您获取当前长度,因此builder.setLength(builder.length() - 4)并且您得到了结果。

But..但..

It's 6, not 4.是6,不是4。

As per your own snippet, you're adding " AND " - note the spaces.根据您自己的代码段,您要添加" AND " - 注意空格。 That's 6, not 4.那是6,不是4。

Swap AND交换和

A much better way to do this is to swap where/and to the front of your appends.一个更好的方法是交换 where/and to your appends 的前面。 Instead of adding AND at the end every time you append another aspect of your query, instead start with AND (for the first entry, WHERE ).每次查询 append 时,不要在末尾添加AND ,而是以AND开头(对于第一个条目, WHERE )。 Now the problem has gone away entirely.现在问题已经完全消失了。 If that WHERE thing is annoying, you're always free to start a query with SELECT stuff FROM table WHERE true and then you can get in the habit of writing each additional query rule as AND thingie = value from there.如果WHERE的事情很烦人,您总是可以自由地使用SELECT stuff FROM table WHERE true开始查询,然后您可以养成将每个附加查询规则编写为AND thingie = value从那里的习惯。

Actually, use JDBI/JOOQ实际上,使用 JDBI/JOOQ

JDBC is extremely low level and isn't really meant to be used like this. JDBC 级别极低,并不是真的要这样使用。 As you found, it's quite annoying to write code for it.如您所见,为它编写代码非常烦人。 That's because JDBC is low-level glue;那是因为JDBC是低级胶; it exists as common ground for DB engine builders and the JVM.它是 DB 引擎制造商和 JVM 的共同点。 Instead, use a library that makes 'talking SQL with databases from java' a lot easier, faster, and more sustainable (easier to maintain, test, and modify in face of change requests).相反,使用一个库,使“使用来自 java 的数据库谈论 SQL”容易、更快、更可持续(面对变更请求更容易维护、测试和修改)。 Such as JDBI or JOOQ .例如JDBIJOOQ

It would be more logical to do less superfluous SQL.少做多余的 SQL 会更合乎逻辑。

    int startWhere = builder.length();
    if (type == EnumTypePrestation.OPTIQUE) {
        builder.append(" and d.idDossier=p.id and d.statut=4");
    }
    if (...) {
        builder.append(" and ...");
    }
    ...
    if (builder.length() > startWhere) {
         builder.replace(startWhere, " and".length(), " where");
    }

This code still is ugly.这段代码仍然很难看。 The attempt to be efficient using a StringBuilder is even error prone.尝试使用 StringBuilder 来提高效率甚至容易出错。

List<String> criteria = new ArrayList<>();
if (type == EnumTypePrestation.OPTIQUE) {
    criteria.add("d.idDossier=p.id and d.statut=4");
}
...
if (!criteria.empty()) {
    builder.append(criteria.stream()
        .collect(Collectors.joining(" and ", " where ", " ");
    // Or:
    builder.append(criteria.stream()
        .collect(Collectors.joining(") and (", " where (", ")");
}

joining has here 3 parameters: separator, prefix and suffix. joining这里有 3 个参数:分隔符、前缀和后缀。

There is one hard advise: do not try to make such code easier with this kind of tiny frameworks.有一个硬性建议:不要尝试使用这种微型框架使此类代码更容易。 This kind of database coding is very often done.这种数据库编码经常被完成。 However the next programmer has to learn it, instead of an existing library.然而,下一个程序员必须学习它,而不是现有的库。 And the chance people program around it or the API will show up deficits, is large.人们围绕它编程或 API 出现缺陷的机会很大。 Better invest the time looking for an existing solution.最好花时间寻找现有的解决方案。 You will save time on maintenance work (new features, difficult things with prepared statements, and so on).您将节省维护工作的时间(新功能、准备好的语句等困难的事情等)。

The answer of rzwitserloot is to the point. rzwitserloot 的答案是中肯的。

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

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