The Problem
I have to copy some multil-line text and save it into a String variable.
Unfortunately, Java doesn't support heredoc or Multi-line String literals.
So if to do this manually, I have to add \r\n at the end of each line, and escape special characters such as ", \ etc.
To do this manually would be quite boring and time wasting.
The Solution
Luckily, Eclipse has a nice and less unknown feature which will escape text for us when pasting text into a string literal.
In Eclipse, go to Preferences > Java > Editor > Typing, check the last option: "Escape text when pasting into a string literal", now when we paste text into a string literal, Eclipse will add \r\n, and escape special characters for us. Is it good, right?
After I pasted the text, Eclipse converted and escaped the string like below:
I have to copy some multil-line text and save it into a String variable.
Unfortunately, Java doesn't support heredoc or Multi-line String literals.
So if to do this manually, I have to add \r\n at the end of each line, and escape special characters such as ", \ etc.
To do this manually would be quite boring and time wasting.
The Solution
Luckily, Eclipse has a nice and less unknown feature which will escape text for us when pasting text into a string literal.
In Eclipse, go to Preferences > Java > Editor > Typing, check the last option: "Escape text when pasting into a string literal", now when we paste text into a string literal, Eclipse will add \r\n, and escape special characters for us. Is it good, right?
After I pasted the text, Eclipse converted and escaped the string like below:
private static final String FORMAT_NEW_TYPE =" <typeDescription>\r\n" + " <name>%s</name>\r\n" + " <description />\r\n" + " <supertypeName>uima.tcas.Annotation</supertypeName>\r\n" + " <features>\r\n" + " <featureDescription>\r\n" + " <name>confidence</name>\r\n" + " <description />\r\n" + " <rangeTypeName>uima.cas.Float</rangeTypeName>\r\n" + " </featureDescription>\r\n" + " </features>\r\n" + " </typeDescription>";