Java: StringBuffer & Concatenation

2020-03-02 01:07发布

I'm using StringBuffer in Java to concat strings together, like so:

StringBuffer str = new StringBuffer();

str.append("string value");

I would like to know if there's a method (although I didn't find anything from a quick glance at the documentation) or some other way to add "padding".

Let me explain; every time I append something to the string, I want to add a space in the end, like so:

String foo = "string value";
str.append(foo + " ");

and I have several calls to append.. and every time, I want to add a space. Is there a way to set the object so that it will add a space automatically after each append?

EDIT --

String input
StringBuffer query = new StringBuffer();
Scanner scanner = new Scanner(System.in);
scanner.UseDelimiter("\n");

do {
   System.out.println("sql> ");

   input = scanner.next();

   if (!empty(input)) query.append(input);

   if (query.toString().trim().endsWith(";")) {
         //run query
   }
}
while (!input.equalsIgnoreCase("exit");

I'll use StringBuilder though as grom suggested, but that's how the code looks right now

6条回答
Luminary・发光体
2楼-- · 2020-03-02 01:23

StringBuffer is final. You cannot derive from it. The Best solution really is to add the padding for yourself. Write a method for it and use a PADDING-Constant so that you can easily change it, or better put it in a parameter.

查看更多
男人必须洒脱
3楼-- · 2020-03-02 01:31

You should be using StringBuilder.

Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

查看更多
唯我独甜
4楼-- · 2020-03-02 01:32

Just add the space yourself, it's easy enough, as per your own example.

查看更多
走好不送
5楼-- · 2020-03-02 01:33

Another possibility is that StringBuilder objects return themselves when you call append, meaning you can do:

str.append("string value").append(" ");

Not quite as slick, but it is probably an easier solution than the + " " method.

Another possibility is to build a wrapper class, like PaddedStringBuilder, that provides the same methods but applies the padding you want, since you can't inherit.

查看更多
对你真心纯属浪费
6楼-- · 2020-03-02 01:38

I think this is handled easier either with a helper method (untested code):

public String myMethod() {
    StringBuilder sb = new StringBuilder();
    addToBuffer(sb, "Hello").addToBuffer("there,");
    addToBuffer(sb, "it").addToBuffer(sb, "works");
}

private StringBuilder addToBuffer(StringBuilder sb, String what) {
    return sb.append(what).append(' ');  // char is even faster here! ;)
}

Or even using a Builder pattern with a fluent interface (also untested code):

public String myMethod() {
    SBBuilder builder = new SBBuilder()
        .add("Hello").add("there")
        .add("it", "works", "just", "fine!");

    for (int i = 0; i < 10; i++) {
        builder.add("adding").add(String.valueOf(i));
    }

    System.out.println(builder.build());
}

public static class SBBuilder {
    private StringBuilder sb = new StringBuilder();

    public SBBuilder add(String... parts) {
        for (String p : parts) {
            sb.append(p).append(' '); // char is even faster here! ;)
        }
        return this;
    }

    public String build() {
        return sb.toString();
    }
}

Here's an article on the subject.

Hope it helps! :)

查看更多
ゆ 、 Hurt°
7楼-- · 2020-03-02 01:42

Can you not create a new class which wraps around StringBuffer and add an appendWithTrailingSpace() method?

CustomStringBuffer str = new CustomStringBuffer();
str.appendWithTrailingSpace("string value");

(Although you may want to call your method something a little shorter.)

查看更多
登录 后发表回答