0

Quick question most likely very simple.

Basically, I need to change a string from obdurate to obliteration. I'm aware of the methods that I can use with StringBuilder but I'm unable to insert anything after the initial string. Is there anyway to add more characters?

my code is as follows

public class SimpleString {

public static void main (String [] args){

StringBuilder s1 = new StringBuilder ("obdurate");

s1.delete(2, 7);
s1.replace(2, 7, "lite");
s1.insert(8,15, "ration");   //the problem line.



System.out.println(s1);
2
  • The code here doesn't even compile (Java 7), so I can't imagine you bothered looking at the javadoc, which explains that the index must be < string.length() (for the actual insert() methods anyway) Commented Nov 21, 2013 at 18:13
  • 1
    please refer this docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html ,the way you call the insert method is wrong Commented Nov 21, 2013 at 18:16

7 Answers 7

3

Use StringBuilder.append(String str)

s1.append("ration");
Sign up to request clarification or add additional context in comments.

Comments

1

Use the append():

s1.append("ration"); 

1 Comment

Append doesn't exist with indexes.
1

you can try the following:

public class SimpleString {

public static void main (String [] args){

StringBuilder s1 = new StringBuilder ("obdurate");

s1.delete(2, 7);
s1.replace(2, 7, "lite");
s1.append("ration");  

System.out.println(s1);

Comments

0
s1.delete(2, s1.length());
s1.append("literation");

Comments

0

Try this

   public static void main(String[] args){

        StringBuilder s1 = new StringBuilder ("obdurate");

        s1.delete(2, 7);
        s1.replace(2, 7, "lite");
        s1.append("ration");

        System.out.println(s1);
    }

Comments

0

Assuming you wanna insert a string into s1, the best way of doing it would be

for (int start = 0, start < 7; start ++) // assuming you wanna insert a string with length 7
    s1.append(" ")
// s1 = "obdurate      " then now you can insert a new string easily
// you can now insert in between (i guess by using insert, you don't just want the string to be added in the end)
s1.insert(3,10, "rationa"); // s1 = "obdrationaurate";

Comments

0

I need to change a string from obdurate to obliteration.

Make use of indexOf(str) with replace(stIndx, endIndx, replacement) function:

StringBuilder builder = new StringBuilder("obdurate it is");
int stIndex = builder.indexOf("obdurate");
int endIndex = stIndex + "obdurate".length(); // Find the end of the replacing string
builder.replace(stIndex, endIndex, "obliteration");

Is there anyway to add more characters?

use strBuilder.append(String) function, where strBuilder is an instance of StringBuilder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.