Java 在另一个字符串的开头插入字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1475807/
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
Insert string in beginning of another string
提问by Gopi
How to insert a string enclosed with double quotes in the beginning of the StringBuilder and String?
如何在 StringBuilder 和 String 的开头插入用双引号括起来的字符串?
Eg:
例如:
StringBuilder _sb = new StringBuilder("Sam");
I need to insert the string "Hello" to the beginning of "Sam" and O/p is "Hello Sam".
我需要将字符串“Hello”插入到“Sam”的开头,O/p 是“Hello Sam”。
String _s = "Jam";
I need to insert the string "Hello" to the beginning of "Jam" and O/p is "Hello Jam".
我需要将字符串“Hello”插入到“Jam”的开头,O/p 是“Hello Jam”。
How to achieve this?
如何实现这一目标?
采纳答案by unwind
回答by cletus
回答by maxy
It is better if you find quotation marks by using the indexof()
method and then add a string behind that index.
如果您使用该indexof()
方法找到引号,然后在该索引后面添加一个字符串,则效果会更好。
string s="hai";
int s=s.indexof(""");
回答by Stephen C
Other answers explain how to insert a string at the beginning of another String
or StringBuilder
(or StringBuffer
).
其他答案解释了如何在另一个String
或StringBuilder
(或StringBuffer
)的开头插入字符串。
However, strictly speaking, you cannot insert a string intothe beginning of another one. Strings in Java are immutable1.
但是,严格来说,您不能将一个字符串插入到另一个字符串的开头。Java 中的字符串是不可变的1。
When you write:
当你写:
String s = "Jam";
s = "Hello " + s;
you are actually causing a new String
object to be created that is the concatenation of "Hello " and "Jam". You are not actually inserting characters into an existing String
object at all.
您实际上String
是在创建一个新对象,该对象是“Hello”和“Jam”的串联。您实际上根本没有将字符插入到现有String
对象中。
1 - It is technically possible to use reflection to break abstraction on String
objects and mutate them ... even though they are immutable by design. But it is a really bad ideato do this. Unless you know that a String
object was created explicitly via new String(...)
it could be shared, or it could share internal state with other String
objects. Finally, the JVM spec clearly states that the behavior of code that uses reflection to change a final
is undefined. Mutation of String
objects is dangerous.
1 - 在技术上可以使用反射来打破对String
对象的抽象并改变它们......即使它们在设计上是不可变的。但这样做是一个非常糟糕的主意。除非你知道一个String
对象是通过new String(...)
它显式创建的,否则它可以被共享,或者它可以与其他String
对象共享内部状态。最后,JVM 规范明确指出使用反射改变 a 的代码行为final
是未定义的。String
对象的突变是危险的。
回答by Zigo Stephen
import java.lang.StringBuilder;
public class Program {
public static void main(String[] args) {
// Create a new StringBuilder.
StringBuilder builder = new StringBuilder();
// Loop and append values.
for (int i = 0; i < 5; i++) {
builder.append("abc ");
}
// Convert to string.
String result = builder.toString();
// Print result.
System.out.println(result);
}
}
回答by Pritam Patil
private static void appendZeroAtStart() {
String strObj = "11";
int maxLegth = 5;
StringBuilder sb = new StringBuilder(strObj);
if (sb.length() <= maxLegth) {
while (sb.length() < maxLegth) {
sb.insert(0, '0');
}
} else {
System.out.println("error");
}
System.out.println("result: " + sb);
}