Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Aliter for string formatting - Which is best approach?

 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[size=12][color=black]Hi,

I am right now looking for a best alternative for the below approach:

I need to format a set of values from DB to be separated with a pipe('|') delimiter (for each record). Right now I am doing string concatenations as such:



I think this approach is very time consuming because of the number of string operations.
Could anyone suggest me the best approach to achieve the same task? I believe we can use pattern for this. Appreciate your help.


Thanks,
Zahid[/color][/size]
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zahid zubair wrote:
I think this approach is very time consuming because of the number of string operations.
Could anyone suggest me the best approach to achieve the same task? I believe we can use pattern for this. Appreciate your help.


I wouldn't worry about this too much. The Java compiler will generate code using a StringBuilder. So, this...



Will be changed to something like this...



Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use Apache's StringUtils.join(collection, "|") if it's in a collection.
 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry/David,

Thanks for your suggestions.

Henry - I feel this is time consuming because This piece of code would execute more than 10,000 times (or may be more than that) per day. Also, my approach as well as your approach are losing readability. I want my application to be more readable and more efficient.

Could you guys help me on this.

David,
I am picking up values from the DB. Actually I want to read the values from DB and make it avbl in a .txt file. Can I write my data row by row or is it best to write it at one shot into the file once I get out of the for loop?

Thanks for your help.

Thanks,
Zahid
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zahid zubair wrote:Henry - I feel this is time consuming because This piece of code would execute more than 10,000 times (or may be more than that) per day. Also, my approach as well as your approach are losing readability. I want my application to be more readable and more efficient.


First, it is *not* my approach. I was just showing you what the compiler does under the covers. As you can see, only one stringbuilder is created in the interim -- there are no interim strings created from that long chain. There isn't really any way to make it more efficient than that... except of course, to not have all those strings, to be concatenated, in the first place.

As for making it more readable.... well, you can separate the lines (and comment). It doesn't make the compiler less efficient by doing so...



Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic