Friday, April 3, 2009

String builder/buffer performance

If you ever wanted to do string manipultion what you would turn to would be one of the following as opposed to using raw String objects which we know is not good for string manipulation due to the object construction on the heap. But the only issue with String builder/buffer is that even those it has append methods, it does not have a clear method which would clear out the buffer and enable the user to start fresh. There are two ways in which you can achieve this;
 
    StringBuilder str = new StringBuilder();
    str.delete(0,str.length);
 
    or
    str.setLength(0);
 
    From the above two methods it is said that performance vice it is always advisable to use the later method of setting the length to zero which will start appending strings from the zeroth position again rather than deleting the already existing strings within the buffer.


No comments:

Post a Comment