Insert string in beginning of another string

JavaString

Java Problem Overview


How to insert a string enclosed with double quotes in the beginning of the StringBuilder and 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".

String _s = "Jam";

I need to insert the string "Hello" to the beginning of "Jam" and O/p is "Hello Jam".

How to achieve this?

Java Solutions


Solution 1 - Java

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

_sb.insert(0, "Hello ");

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

String s2 = "Hello " + _s;

Solution 2 - Java

Other answers explain how to insert a string at the beginning of another String or StringBuilder (or StringBuffer).

However, strictly speaking, you cannot insert a string into the beginning of another one. Strings in Java are immutable1.

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.


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 idea to 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.

Solution 3 - Java

Sure, use StringBuilder.insert():

_sb.insert(0, _s);

Solution 4 - Java

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);

    }

Solution 5 - Java

You can add a string at the front of an already existing one. for example, if I have a name string name, I can add another string name2 by using:

name = name2 + name;

Don't know if this is helpful or not, but it works. No need to use a string builder.

Solution 6 - Java

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);
    }
}

Solution 7 - Java

It is better if you find quotation marks by using the indexof() method and then add a string behind that index.

string s="hai";
int s=s.indexof(""");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGopiView Question on Stackoverflow
Solution 1 - JavaunwindView Answer on Stackoverflow
Solution 2 - JavaStephen CView Answer on Stackoverflow
Solution 3 - JavacletusView Answer on Stackoverflow
Solution 4 - JavaPritam PatilView Answer on Stackoverflow
Solution 5 - JavaDan BurrView Answer on Stackoverflow
Solution 6 - JavaZigo StephenView Answer on Stackoverflow
Solution 7 - JavamaxyView Answer on Stackoverflow