2

How can I refactor substring concatenations to StringBuilder?

        String line = "this is my start and my end string";
        int start = line.indexOf("start");
        line = line.substring(0, start) + line.substring(start + 5);

        int end = line.indexOf("end");
        line = line.substring(0, end) + line.substring(end + 3);
            //result: this is my and my string";

How would the same code look like with StringBuilder? When I want to execute substrings several times one after the other?

1
  • Have you considered using regular expression? Commented Oct 1, 2013 at 10:02

5 Answers 5

4

If your question is "How do I repeatedly remove substrings from a string", you might try something like this:

StringBuilder line = new StringBuilder("this is my start and my end string");
int start = line.indexOf("start");
line.delete(start, start + 5);
int end = line.indexOf("end");
line.delete(end, end + 3);
String result = sb.toString()

Create the StringBuilder once, and then manipulate its contents.

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this may be:

    StringBuilder line = new StringBuilder("this is my **start** and my **end** string");
    int start = line.indexOf("start");
    line = new StringBuilder(line.substring(0, start).append(line.substring(start + 5)));

    int end = line.indexOf("end");
    line = new StringBuilder(line.substring(0, end).append(line.substring(end + 3)));

2 Comments

You've declared the variable start two times
Thanks, corrected. Sorry it was copy/paste from OP's original post.
1

Something like this you mean? Or have I understood it wrong?

String line = "this is my start and my end string";
int start = line.indexOf("start");
int end = line.indexOf("end");
StringBuilder sb = new StringBuilder();
sb.append(line.substring(0, start);
sb.append(line.substring(start + 5);
sb.append(line.substring(0, end);
sb.append(line.substring(end + 3);
String result = sb.toString()

Comments

0

Try

String line = "this is my start and my end string";
    int start = line.indexOf("start");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(line.substring(0, start));
    stringBuilder.append(line.substring(start + 5));

    int end = line.indexOf("end");
    stringBuilder.append(line.substring(0, end));
    stringBuilder.append(line.substring(end + 3));
    System.out.println("StringBuilder : "+stringBuilder.toString());

Comments

0

StringBuilder has it's Own indexOf and substring implementations if i am not wrong. The primary concern of the question seems to discard the use of String, and main features of StringBuilder are still concatenations.

So http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html can help you find relevant methods, and you can go on using StringBuilder with .append() method and ur string finding and substring methods.

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.