Tuesday, August 17, 2021

How to Escape String Literal in Java Using Eclipse IDE? Example

Whenever you paste String in Eclipse which contains escape characters,  to store in a String variable or just as String literal it will ask to manually escape special characters like single quotes, double quotes, forward slash, etc. This problem is more prominent when you are pasting a large chunk of data which contains escape characters like a whole HTML or XML file,  which contains lots of single quotes e.g. ‘’ and double quotes “” along with forwarding slash on closing tags. It’s very hard to manually escape all those characters and it's pretty annoying as well.

While writing JUnit test for XML documents, parsing, and processing I prefer to have the whole XML file as String in the Unit test, which pointed me to look for that feature in Eclipse.

As I said earlier in my post Top 30 Eclipse keyboard shortcuts, I always look to find new shortcuts and settings in Eclipse which help to automate repetitive tasks. Thankfully Eclipse has one setting which automatically escapes text as soon as you paste it. 

This is even more useful if you prefer to copy the file path and just paste it, Since windows use forward slash it automatically escape that instead of you going manually and escaping them. By default, this setting is disabled in Eclipse IDE and you need to enable it.

Btw, if you are a beginner, I suggest you first go through a beginner course like Eclipse Tutorials for Beginners to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.


How to enable automatic escaping while pasting text as String literal in Eclipse :

Here are the steps you need to perform to enable this setting in Eclipse which will automatically provide escaping required in Java for special characters like quotes, slashes, etc.



1. Open Eclipse IDE
2. Go to Windows --> Preferences --> Java --> Editor --> Typing 
3) check the check box "Escape text when pasting into a String literal" on section "In String literals.

This will escape text when pasted as String literal. Do it now, it's an option worth enabling and I just wonder why not Eclipse IDE makes this option enabled by default. Believe me, it's extremely useful but same time hard to discover. 

Here is a screenshot of this option.  As you can see, by default this option is not enabled.

How to escape text when paste as String literal Eclipse Tips

Next time no need to extra edit any XML or HTML text before pasting it as String literal in Eclipse IDE. The eclipse will do it for you automatically. Once again, if you are doing anything manually and think that it would be good Eclipse can assist on that task, look for it using google or Eclipse help. There is a good chance you can discover a useful Eclipse shortcut or settings.


Other Eclipse tutorials and tips for Java programmer

6 comments :

Anonymous said...

Very Informative..

nabil said...

thanks man you saved my day

Unknown said...

Thanks man.. It really save my time. Great help.. :) :)

Anonymous said...

Thanks, I wanted the opposite: pasting \t UNchanged to \\t

javin paul said...

Just don't use this shortcut? doesn't that work?

Anonymous said...

Thank you. This was a big time saver

Post a Comment