Can anyone please tell me how I can use if-else condition with StringBuffer to check if it is empty?
I am expecting something like this
if (Stringbuffer is empty){
// some condition
}
else {
// some other condition
}
Can anyone please tell me how I can use if-else condition with StringBuffer to check if it is empty?
I am expecting something like this
if (Stringbuffer is empty){
// some condition
}
else {
// some other condition
}
[returns] the length of the sequence of characters currently represented by this object
so you can use the fact that StringBuffer.length()
returns zero (or not) to check if the buffer is empty (or not).
Note that you should probably be using StringBuilder
instead of StringBuffer
. From the Javadoc of StringBuffer
:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,
StringBuilder
. TheStringBuilder
class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
StringBuffer buffer = null;
if(buffer == null || buffer.length() == 0){
// some condition
}else{
// some other condition
}