It is still constructing a new object, which is expensive.
Tahir Akhtar wrote:
You are right!!! It even says that in comments above constructor.
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex -
beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}
// Package private constructor which shares value array for speed.
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
-----Original Message-----
From: Steffen Heil [mailto:[EMAIL PROTECTED]
Sent: Friday, January 26, 2007 3:04 PM
To: 'Jakarta Commons Users List'
Subject: RE: isBlank implementation question for educational purposes,
please
Hi
Actually his will be slower even if it's blank. Strings are
immutable (meaning they can't be changed), so under the hood
the trim() method will have the overhead of creating a new
String object. Then it will have to cycle through each
character in the original String to copy the non-trimmed
characters into the new String instance. So even if it was
blank it has the overhead of checking every character just to
copy them into the new String that it will return.
No.
substring does not copy Strings.
It just returns new String objects, that refer to the original char[], but
have different offset and length values.
I haven't looked at the implementation right now, but I assume, it justs
counts isSpace(s) from the start and from the end and calls substring.
Therefor it's execution time should be linear to the amount of spaces at the
beginning and end and be independant from the length of the rest of the
String.
Regards,
Steffen
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]