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>");

LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
 
Bart CremersJava ArchitectCommented:
or

String line = "<ExpiryDate>" + cardEnd + "</ExpiryDate>";
line = line + "<StartDate>" + cardStart + "</StartDate>";
0
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.