Java 如何从用户那里获取 StringBuilder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18087951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:18:19  来源:igfitidea点击:

How to get StringBuilder From user

java

提问by gandalf

how can i get StringBuilderfrom users through Scannerclass?? or any other classes? i know that we can get a string and then put it in a StringBuilderlike this:

我如何StringBuilder通过Scanner课堂从用户那里获得?或任何其他课程?我知道我们可以得到一个字符串,然后StringBuilder像这样把它放进去:

import java.util.Scanner;

public class Sade {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String str;

        str = scan.nextLine();
        StringBuilder strb  = new StringBuilder(str);
    }
}

or through using append(). but is there anyway to get StringBuilderdirectly from user??

或通过使用append(). 但无论如何可以StringBuilder直接从用户那里获得?

采纳答案by ddavison

You cannot fetch a StringBuilderobject from the Standard Input, but you can output to the user enter the strings (type 'exit' to exit):and formulate your own.

您无法StringBuilder从标准输入中获取对象,但您可以输出给用户enter the strings (type 'exit' to exit):并制定您自己的内容。

Loop through this until they type 'exit', and each time you fetch the string, you can do myStringBuilder.add(theirInput)
(this, of course, is pseudo-code)

循环遍历直到他们输入“exit”,每次获取字符串时,您都可以这样做myStringBuilder.add(theirInput)
(这当然是伪代码)

回答by Mike Thomsen

You misunderstand the purpose of StringBuilder. String concatenation requires creating a new Stringwhich causes unnecessary garbage collection. StringBuilderappends multiple strings together by putting their contents in a character buffer and then returning a new string all at once. This causes a lot of garbage collection:

你误解了 的目的StringBuilder。字符串连接需要创建一个新的String,这会导致不必要的垃圾回收。StringBuilder通过将多个字符串的内容放入字符缓冲区,然后一次返回一个新字符串,将多个字符串附加在一起。这会导致大量垃圾收集:

while ( (line = scanner.nextLine()) != null)
    dataSoFar += line;

Each iteration through that causes the old String object referenced by dataSoFarto be concatenated into a new Stringwith line. StringBuildermerely reads lineand puts its characters onto the end of the internal buffer. When it needs to, it'll resize the buffer which requires a lot less GC work when reading a big source of character data.

每次迭代都会导致引用的旧 String 对象dataSoFar连接成一个新的Stringwith lineStringBuilder只是读取line并将其字符放在内部缓冲区的末尾。当需要时,它会调整缓冲区的大小,这在读取大量字符数据时需要更少的 GC 工作。

Java's API doesn't return you a string builder because it would make no sense here.

Java 的 API 不会返回一个字符串生成器,因为它在这里没有意义。

回答by John Snow

You misunderstand the StringBuilderClass.The principal operations of a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point. The best thing you can do with your code is :-

你误解了StringBuilder 类。StringBuilder 的主要操作是 append 和 insert 方法,它们被重载以接受任何类型的数据。每个都有效地将给定的数据转换为字符串,然后将该字符串的字符附加或插入到字符串构建器中。append 方法总是在构建器的末尾添加这些字符;insert 方法在指定点添加字符。你可以用你的代码做的最好的事情是:-

import java.util.Scanner;

    public class sb {

        public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);
            StringBuilder strb  = new StringBuilder(scan.nextLine());
            strb.append( scan.nextLine());
            System.out.println(strb);
        }
    }

Ofcourse,it is the same as you posted!

当然和你发的一样!

回答by ja_mesa

If what you want is to read all lines type in by the user into a StringBuilder this can be accomplished with something like this:

如果您想要将用户输入的所有行读取到 StringBuilder 中,则可以通过以下方式完成:

Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String text=null;
while ((text = scan.nextLine ()) != null)
{
      sb.append(text);
}