传递和返回 StringBuilders:Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5278204/
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
Passing and Returning StringBuilders: Java
提问by DBguy
I was referred to this site by a friend. I like that I was able to look at some similar threads before I started this thread. Unfortunately, I didn't see anything about this specific issue.
我被一个朋友推荐到这个网站。我喜欢在我开始这个线程之前能够查看一些类似的线程。不幸的是,我没有看到有关此特定问题的任何信息。
I'm having an issue passing StringBuilders from object to object in Java. I don't know what the problem is and am lost. I can pass and return data all day. With StringBuilders, it just doesn't work. I'm thinking it's something really easy that i'm simply too frustrated to see.
我在 Java 中将 StringBuilders 从一个对象传递到另一个对象时遇到问题。我不知道问题是什么,我迷路了。我可以整天传递和返回数据。使用 StringBuilders,它就行不通了。我认为这是一件非常简单的事情,我只是太沮丧而无法看到。
I have a homework assignment which asks me to declare 3 Stringbuilders with my first name, middle name, and last name. No problem.
我有一个家庭作业,要求我用我的名字、中间名和姓氏声明 3 个 Stringbuilder。没问题。
It also wants 3 objects to do different formats of the name. The formatting portion is easy. I can't figure out how to return the work back to the main.
它还希望 3 个对象做不同格式的名称。格式化部分很容易。我不知道如何将工作返回到主要部分。
Here's a snippet:
这是一个片段:
public class Builder
{
public static void main(String[] args)
{
StringBuilder str1 = new StringBuilder("John");
StringBuilder str2 = new StringBuilder("Que");
StringBuilder str3 = new StringBuilder("Doe");
??? = entireName(str1, str2, str3);
}
public static String entireName(StringBuilder s1, StringBuilder s2, StringBuilder s3)
{
System.out.print(s1);
//insert.s1('4', ' ');//format stuff, not really necessary (not a problem).
Ststem.out.print(s2);
//etc..
System.out.print(s3);
return ????;
}
}
the question marked stuff isn't filled in because nothing I put in there worked anyway. I've tried declaring separate strings to pass back. I've tried declaring the strings, then using those strings in the stringbuilder. I've got nothing. The formatting portion of this stuff is easy. If I can't return to the main, it's wrong.
问题标记的东西没有填写,因为我在那里放的任何东西都没有用。我试过声明要传回的单独字符串。我试过声明字符串,然后在 stringbuilder 中使用这些字符串。我什么都没有 这个东西的格式化部分很容易。如果我不能回到主要,那就错了。
I really appreciate any help. Thank you!
我真的很感激任何帮助。谢谢!
回答by corsiKa
Assuming your prints are for debugging, you need s1.toString() and such
假设您的打印用于调试,您需要 s1.toString() 等
public static String entireName(StringBuilder s1, StringBuilder s2, StringBuilder s3)
{
System.out.print(s1.toString());
//insert.s1('4', ' ');//format stuff, not necessary (not a problem)
System.out.print(s2.toString());
//etc..
System.out.print(s3.toString());
return s1.append(s2).append(s3).toString();
}
I ran this, and it works:
我运行了这个,它有效:
public class Builder
{
public static void main(String[] args)
{
StringBuilder str1 = new StringBuilder("John");
StringBuilder str2 = new StringBuilder("Que");
StringBuilder str3 = new StringBuilder("Doe");
String s = entireName(str1, str2, str3);
System.out.println(s);
}
public static String entireName(StringBuilder s1, StringBuilder s2,
StringBuilder s3)
{
// debugging removed
return s1.append(s2).append(s3).toString();
}
}