将字符串附加到另一个字符串的开头的方法是什么?

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

Way to append strings to the start of another String?

问题

我试图创建一个记分板,并在现有的方框上附加或添加另一个方框。

String res = "";
res += "+-----+-----+\n";
for (int i = 0; i < 4; ++i) {
    res += "|     |     |\n";
}
res += "+-----+-----+";

在顶部和底部打印出+-----+,两侧为 | |。

我想根据输入的变量数量在顶部添加 -----+,然后总共添加 | | 两侧,以创建:

+-----+-----+
|     |     |
|     |     |
|     |     |
|     |     |
+-----+-----+

两个方框。第二个方框连接到第一个方框上。
程序在一个方法中完成,因此我只能返回一个字符串。我希望只使用现有的方框,然后将 -----+ 连接到顶部,将 | | 连接到两侧,但原始的方框程序在底部结束。是否有一种方法可以使它从顶部重新开始,这样我就可以向现有方框添加其他字符串?

我目前添加另一个方框的代码是重新创建第一个方框,然后尝试添加额外的内容,但它不起作用,因为它将附加内容放在底部,而第一个 | | 总是连接在一起,而不是相邻。

+-----+-----+|     |
|     |
|     |
|     |
+-----+|     |
|     |
|     |
|     |
+-----+
英文:

Im trying to create a scoreboard and append or add another box to the existing box.

String res = &quot;&quot;;
    res += &quot;+-----+\n&quot;;
    for (int i = 0; i &lt; 4; ++i) {
        res += &quot;|     |\n&quot;;
    }
    res += &quot;+-----+&quot;;

Prints out +-----+ at top and bottom, with | | on the sides.
Im trying to add -----+ to the top, dependent on how many of a variable is input. And then add the | | sides in total to make:

+-----+-----+
|     |     |
|     |     |
|     |     |
|     |     |
+-----+-----+

2 boxes. The second one is attached to the first one.
The program is being done in a method, so I can only return 1 string. I would like to just use the existing box, and concat the -----+ to the top and | | to the sides, but the original box program ends at the bottom. Is there a way to start it back to the top, so I could add the other strings to the existing box?

My current code for adding another box is the recreate the first box, and try to add the extra stuff but its not working because it puts the additions on the bottom, and the first | | is always attached instead of adjacent.

+-----+-----+|     |
|     |
|     |
|     |
+-----+|     |
|     |
|     |
|     |
+-----+ 


</details>


# 答案1
**得分**: 2

要在字符串开头添加字符串:

```java
String str = "Hello";
str = "at start " + str;
System.out.println(str);  // 输出 "at start Hello"

没有特定的快捷操作符(如 +=)来执行此操作。但你不需要操作符。

话虽如此,如果你在循环中连接字符串,使用 StringBuilder 显式地进行连接通常会更加高效。


1 - 此操作的正确术语是 "prepend"。"Append" 这个词表示在末尾添加。"在开头添加" 是一种矛盾的说法。

英文:

To append<sup>1</sup> a string at the start of a string:

String str = &quot;Hello&quot;;
str = &quot;at start &quot; + str;
System.out.println(str);  // outputs &quot;at start Hello&quot;

There isn't a specific shortcut operator (like +=) to do this. But you don't need an operator.

Having said that, if you are concatenating strings in a loop it is often significantly more efficient to use a StringBuilder explicitly.


<sup>1 - The correct term for this is "prepend". The word "append" means add at the end. "Append at the start" is a contradiction.</sup>

huangapple
  • 本文由 发表于 2020年10月26日 06:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529624.html
匿名

发表评论

匿名网友

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

确定