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

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

Insert string in beginning of another string

javastring

提问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

The first case is done using the insert()method:

第一种情况是使用以下insert()方法完成的:

_sb.insert(0, "Hello ");

The latter case can be done using the overloaded + operator on Strings. This uses a StringBuilder behind the scenes:

后一种情况可以使用字符串上的重载 + 运算符来完成。这在幕后使用 StringBuilder :

String s2 = "Hello " + _s;

回答by cletus

Sure, use StringBuilder.insert():

当然,使用StringBuilder.insert()

_sb.insert(0, _s);

回答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 Stringor StringBuilder(or StringBuffer).

其他答案解释了如何在另一个StringStringBuilder(或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 Stringobject to be created that is the concatenation of "Hello " and "Jam". You are not actually inserting characters into an existing Stringobject at all.

您实际上String是在创建一个新对象,该对象是“Hello”和“Jam”的串联。您实际上根本没有将字符插入到现有String对象中。



1 - It is technically possible to use reflection to break abstraction on Stringobjects and mutate them ... even though they are immutable by design. But it is a really bad ideato do this. Unless you know that a Stringobject was created explicitly via new String(...)it could be shared, or it could share internal state with other Stringobjects. Finally, the JVM spec clearly states that the behavior of code that uses reflection to change a finalis undefined. Mutation of Stringobjects 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);

    }