Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

reduce the lines of StringBuffer code

Hi

Just trying to reduce the size of my pages and make them more readable and wondered how I can bring items like the example below into 1 line

strb.append ("<ExpiryDate>");
strb.append (cardEnd);
strb.append ("</ExpiryDate>");

can I do something like :
strb.append ("<ExpiryDate>" + cardEnd + "</ExpiryDate>");

0
Neil Thompson
Asked:
Neil Thompson
1 Solution
 
brunoguimaraesCommented:
You can do that.

You can also do:

strb.append ("<ExpiryDate>").append (cardEnd).append ("</ExpiryDate>");
0
 
objectsCommented:
can I do something like :
strb.append ("<ExpiryDate>" + cardEnd + "</ExpiryDate>");

u can but it defeats the purpose of StringBuffer.
U should also look at using StringBuilder instead of StringBuffer
0
 
Bart CremersJava ArchitectCommented:
You can simply write this as:

String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>";

The modern Java compilers will optimize this easily to using StringBuilders (StringBuffers) as needed. And you get the benefit that future compilers might use better optimization when available.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zzynxSoftware engineerCommented:
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Ok

So if I was to use the String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>"; method how would I add additional content to this?

Something like: ??
String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>";
+line = "<StartDate>" + cardStart + "</StartDate>";

Sorry but this may be easy but I'm winging it a bit at present

Neil
0
 
Bart CremersJava ArchitectCommented:
String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>";
line += "<StartDate>" + cardStart + "</StartDate>";
0
 
Bart CremersJava ArchitectCommented:
or

String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>";
line = line + "<StartDate>" + cardStart + "</StartDate>";
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Thank you all for your help

I've accepted this one as I am a beginner and the code serves 2 perposes for me
1) it keeps it easy (cant get much easier than string)
2) keeps my code clean

Some of you may suggest this is the wrong way but I'm trying to alter code that has been left behind by another developer and struggelling to find my way round Java (I'm a PHP guy) .

I do learn from all your comments though so tons of thanks, without you lot people like me would never learn!

Thanks
Neil
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now