Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write a long string

Status
Not open for further replies.

developerinlondon

Programmer
Jan 11, 2003
196
GB
whats the easiest way to write a long string in java?

eg:

String s = "blah blah
blah blah
blah blah
blah blah
...
...

";

any ideas?

thanks!
 
You definitely want to use a StringBuffer if you're concatenating Strings.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I think you have to concatenate them anyway to span lines.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
You cannot have a String that spans multiple lines like you have suggested in your first post - its just not allowed syntax.

The closest you can get is :

String s = "blah blah" +
"blah blah" +
"blah blah" +
"blah blah" +
"..." +
"..." +
"";

Though I'm guessing that that is not what you want - and that you want CRLF characters - which you will need to add yourself.

I'd use a StringBuffer.

--------------------------------------------------
Free Database Connection Pooling Software
 
My question is what you need a long String for.

Anyway

Code:
StringBuffer sb = new StringBuffer(10 * INFINITUM);
for (int i=0;i<INFINITUM;i++)
  sp.append("Line ").append(i).append("\n");

Cheers,

Dian

 
i can need it for various things.
eg at the moment i was writing a test script that would send a bit of xml data across the network and read its response. on a test script i would rather have an easy to cut and paste xml, instead of cut and paste and put " and put + etc.

i think this is a very limiting factor of java language if there is no way to represent a multiline string in one definition.

i did get around the problem by putting the xml in a file and reading the file. so i need to change the file whenever i want to change the test xml data.... although this is still going a long way for no reason.
 
i think this is a very limiting factor of java language if there is no way to represent a multiline string in one definition.

How so ? You can achieve the same with the method I showed you, or by using a StringBuffer.

You'll find multiline Strings (in the way you mean) are deprecated also in ANSI C/C++ also ...

--------------------------------------------------
Free Database Connection Pooling Software
 
I still don't catch the idea

Code:
String multiLine = "I'm\na\nmultiline\nString";

Cheers,

Dian
 
Code:
<?xml version="1.0" encoding="utf-8"?>
		<soap:Envelope xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:soap="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/">[/URL]
			<soap:Body>
				<QuoteRequest xmlns="[URL unfurl="true"]https://www.bcponline.co.uk">[/URL]
					<header>
						<SourceCode>BN325</SourceCode>
						<MessageSource>B2B3</MessageSource>
						<Profile>B08A</Profile>
						<Password>AP3U86V</Password>
						<OperatorID>flightline</OperatorID>
						<ShowWebOptions>0</ShowWebOptions>
					</header>
		     		<serviceSelection>
						<ServiceProviderCode></ServiceProviderCode>
						<ProductCode>CarParking</ProductCode>
						<IATACode>lgw</IATACode>
					</serviceSelection>
					<quoteDetails>
						<DepartureDate>21-Jun-2005</DepartureDate>
						<DepartureTime>07:00</DepartureTime>
						<ReturnDate>28-Jun-2005</ReturnDate>
						<ReturnTime>07:00</ReturnTime>
						<QuoteReference></QuoteReference>
						<NoPax>1</NoPax>
					</quoteDetails>
					<sPostCode></sPostCode>
				</QuoteRequest>
			</soap:Body>
		</soap:Envelope>
so if we use stingbuffer to write the above, can you do it in 2 seconds with a copy and paste? and 2 seconds later I need to change it to another xml, and change it a few times more... I am just saying its an added inconvenience to not to be able to represent these on a AS IS basis.
 
Write an utility to read the file to a String, tht will take you ten minutes.

What you're seeing is the visualization of the content of a file. I think it's an advantage that Java does not interpret the "AS IS".

Anyway, xml ignores line breaks, doesn't it?

Cheers,

Dian
 
Reading from a file is the way I would go too, because you don't have to escape the '"'.

And you needn't recompile, if you like to change the xml.

And it's far more readable, than such a big chunk of xml in your code.

Or read from stdin, and use
Code:
cat test.xml | java YourClass

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top