Home > Eclipse, Tools > Easy pasting of strings into Java code using Eclipse

Easy pasting of strings into Java code using Eclipse


Ever tried to copy a PATH to your java code and end up needing to go into it and replacing all ‘\’ with ‘\\’ ?

Well – a coworker showed me a cool feature in Eclipse that automatically escapes text when pasting it , just go to:
Window –> Preferences –> Java –> Editor –> Typing and check the ‘Escape text when pasting into a string literal’ option. (See below image)
Now whenever you paste text into a string literal (anything that starts with “ and ends with a “) – Eclipse will automatically escape it for you.

This also works for ‘\n’ and any other escape character.

Example of what it does:
Lets say you want to print this text –

Hello everyone!
 this is a "sample" of pasting a '"' and a '\' in a string

In the code it will look like:

String text = "Hello everyone!\n" + "this is a \"sample\" of pasting a '\"' and a '\\'  in a string"

Note: This was written for Eclipse Indigo (3.7), future/past versions may differ.

Advertisements
Categories: Eclipse, Tools Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: