How to put spaces in a stringbuilder
Asked Answered
M

5

15

Hello I have following method to display a promotion line when I comment a shoutbox:

public String getShoutboxUnderline(){
        StringBuilder builder = new StringBuilder();
        builder.append("watch");
        builder.append("on");
        builder.append("youtube");
        builder.append(":");
        builder.append("Mickey");
        builder.append("en");
        builder.append("de");
        builder.append("stomende");
        builder.append("drol");

        return builder.toString();
    }

But when I get it, I get watchonyoutube:mickeyendestomendedrol, which is without spaces. How do I get spaces in my Stringbuilder?

Merideth answered 26/1, 2016 at 12:29 Comment(0)
M
66

As of JDK 1.8, you can use a StringJoiner, which is more convenient in your case:

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

StringJoiner joiner = new StringJoiner(" "); // Use 'space' as the delimiter
joiner.add("watch") // watch 
      .add("on") // watch on 
      .add("youtube") // watch on youtube
      .add(":") // etc...
      .add("Mickey")
      .add("en")
      .add("de")
      .add("stomende")
      .add("drol");

return joiner.toString();

This way, you will not need to add those spaces "manually".

Malapert answered 26/1, 2016 at 12:32 Comment(1)
Note: This solution is valid only if you're using Java 1.8 and upImbalance
M
7

Just invoke builder.append(" ") at the location of your preference.

E.g.

builder
    .append("watch")
    .append(" ")
    .append("on")

...etc.

NB:

  • Using the fluent builder syntax here for convenience
  • You can also just append a space after each literal instead (save for the last one)
Muttonchops answered 26/1, 2016 at 12:30 Comment(0)
T
2

Cleaner way of doing it.

Create a class variable:

private static final String BLANK_SPACE=" ";

Now in you StringBuilder code ,append it where required:

StringBuilder builder = new StringBuilder();
    builder.append("watch");
    builder.append(BLANK_SPACE);
    builder.append("on");
    builder.append("youtube");
    builder.append(":");
    builder.append(BLANK_SPACE);
    builder.append("Mickey");
    builder.append("en");
    builder.append("de");
    builder.append(BLANK_SPACE);
    builder.append("stomende");
    builder.append("drol");
    System.out.println(builder.toString());
Trinhtrini answered 26/1, 2016 at 12:44 Comment(1)
You only write variables in capslock if they are constants. If they are you best use private static final String BLANK_SPACE = " ";.Sevastopol
B
1

A space is only a string containing the single character space.

So you can append it exactly as appending any other string.

    StringBuilder builder = new StringBuilder();
    builder.append("watch");
    builder.append(" ");
    builder.append("on");
    builder.append(" "); 
    // and so on

Remember also that the append method returns the StringBuilder so it is possible to join appends one after the other as follow

    StringBuilder builder = new StringBuilder();
    builder.append("watch").append(" ");
    builder.append("on").append(" "); 
    // and so on
Brownson answered 26/1, 2016 at 12:46 Comment(0)
E
0

You can use this, it's equivalent to using StringBuilder or StringJoiner, but smaller

public class StringUnifier {

    String separator = "";
    List<String> dataList = new ArrayList<>();
    private String response = "";

    StringUnifier(String separator) {
        this.separator = separator;
    }

    StringUnifier add(String data) {
        if (!data.isEmpty()) {
            this.dataList.add(data);
        }
        return this;
    }

    @Override
    public String toString() {
        this.dataList.forEach(str -> {
            this.response += (str + this.separator);
        });
        return this.response.substring(0, this.response.length() - this.separator.length());
    }

}

MAIN

public class Main_Test {

    public static void main(String[] args) {
        StringUnifier stringUnifier = new StringUnifier(" ");
        stringUnifier.add("columna1").add("columna2").add("columna3");
        System.out.println(stringUnifier.toString());
    }
}

RUN

output:

columna1 columna2 columna3
Eusebioeusebius answered 29/6, 2022 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.