Fred Hamilton wrote:if my for loop is of the form for( int i=0; i<sb.length(); i++ ) {...}, and the length of the StringBuilder decreases within the loop body, will the termination condition of the loop adapt, i.e. is sb.length() re-evaluated with each iteration of the loop?
If anyone has any ideas for a cleaner solution than the one I am considering, I'm all ears.
There are only 10 types of people in the world: those who understand ternary, those who don't, and those who mistake it for binary.
Henry Wong wrote:
Fred Hamilton wrote:if my for loop is of the form for( int i=0; i<sb.length(); i++ ) {...}, and the length of the StringBuilder decreases within the loop body, will the termination condition of the loop adapt, i.e. is sb.length() re-evaluated with each iteration of the loop?
If anyone has any ideas for a cleaner solution than the one I am considering, I'm all ears.
How about, instead of copying the string to the string builder, don't do that part. Only use the string builder for the result. Use the for loop on the string itself, and copy the chars to the builder as you are processing it.
And of course, a simple solution based on regex is possible here too.
Henry
Abhishk Gupta wrote:well...I think I have some readymade solution...
Fred Hamilton wrote:
Thanks Henry, I actually have a solution that does use a second StringBuilder more or less like you suggest, but it's ugly. . But I was looking for something a little cleaner. So the question still remains about whether or not the termination condition will adapt to the changing length of the StringBuilder.
Fred Hamilton wrote:
As for the RegEx solution, I tried to think of one but... hmmm... well I suppose I could do a replaceAll, where the replacement string is a method call that returns the string representation of the length of the substring that matches the pattern. I don't know if that would work, but it's worth a try.
Henry Wong wrote:
Fred Hamilton wrote:
Thanks Henry, I actually have a solution that does use a second StringBuilder more or less like you suggest, but it's ugly. . But I was looking for something a little cleaner. So the question still remains about whether or not the termination condition will adapt to the changing length of the StringBuilder.
I can't image it being "ugly". Heck, you don't need a second string buffer -- as the original string can be the source.
...
Fred Hamilton wrote:
Any thoughts about the termination condition issue? That's the real question here.
Henry Wong wrote:Abhishk, it is generally *not* a good idea to give "read made solutions" -- as the JavaRanch is a learning site, and there isn't much learning when you are given the answer.
Henry Wong wrote:
If you want to attempt a regex solution, I would recommend that you look at the find(), appendReplacement(), and appendTail() methods. The replaceAll() method is a bit too high level to do this operation efficiently (meaning it won't be able to do this in a single pass).
There are only 10 types of people in the world: those who understand ternary, those who don't, and those who mistake it for binary.
Henry Wong wrote:
Fred Hamilton wrote:
Any thoughts about the termination condition issue? That's the real question here.
The "terminating condition" is the end of the string. Remember, if you use the original string as the source, and the stringbuffer as the target, the underlying string doesn't change while you are processing it.
Henry
Abhishk Gupta wrote:As far as regex solution suggested by you is considered, I am not much comfortable with that.
Henry Wong wrote:
If you want to attempt a regex solution, I would recommend that you look at the find(), appendReplacement(), and appendTail() methods. The replaceAll() method is a bit too high level to do this operation efficiently (meaning it won't be able to do this in a single pass).
I think even this would involve complexities of customizing Matcher and Pattern classes.
This is just my opinion...Please share your thoughts on it.
When you do things right, people won't be sure you've done anything at all.
Henry Wong wrote:
Here is how I would do this problem, using regexes...
There are only 10 types of people in the world: those who understand ternary, those who don't, and those who mistake it for binary.
D. Ogranos wrote:My solution would be
Should be equivalent to Henry's, except that it is much more efficient. Regular expressions are SLOOOOOOOOOOOW![]()
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|