0

Have and input of this format:

<table>
<tbody>
    <tr bgcolor='#999999'>
        <td nowrap width='1%'>
        </td>
        <td nowrap width='3%' align='center'>
            <font style='font-size: 8pt'> System ID </font>
        </td>
        <td nowrap width='5%' align='center'>   

In order to remove nowrap attribute , was earlier using this code:

    if (deletedString == null)
    {
        return exportedTable;
    }

    int tagPos = 0;
    String resultTable = exportedTable;
    while (resultTable.indexOf(deletedString) != -1)
    {
        tagPos = resultTable.indexOf(deletedString, tagPos);
        String beforTag = resultTable.substring(0, tagPos);
        String afterTag = resultTable.substring(tagPos + deletedString.length());

        resultTable = beforTag + afterTag;
    }
    return resultTable;

deletedString is nowrap, and input is exportedTable. But this is causing Performance issues. Is there any better way to do it?

2 Answers 2

1

My recommendation: StringUtils.remove(source, substring) will remove all instances of the substring from the source string. This answer benchmarked this method and found it to be five times faster than a few alternatives.

Alternatively, use a StringBuilder to aggregate your substrings - every time you concatenate two strings you're creating a new string, whereas StringBuilder is mutable and doesn't need to create a new copy on an update.

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

1 Comment

I will try with the first approach as u suggested. For the second one, actually the performance lag is more due to the no of iterations of while loop, than the String concatenation part.
0

You could create a xmlstreamreader and have a while loop that parses through the xml as long as the streamreader.hasNext().

Format:

//Create stream reader
//Position at beginning of document

//While the stream reader has next (can see next line)
//perform action 

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.