Reputation: 4093
I'm fairly sure this is a bug in the JavaFX clipboard but I want to make sure I'm not doing something stupid. I'm programatically placing plain text on to the clipboard using the following code:
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
//String test = "1" + System.lineSeparator() + "2"; //Example 1 - Two lines
//String test = "1\r\n2"; //Example 2 - Two lines
String test = "1\n2"; //Example 3 - One line
content.putString(test);
clipboard.setContent(content);
Example 1 and 2 result in this text after pasting
1
2
Example 3 results in this text after pasting (as expected)
1
2
Making notepad++ show line ends confirms that in the first two example the lines endings are being doubled. Running a debugger over it shows the String is fine after it's been placed into the ClipboardContent but I stopped following it after that.
This is all on Windows 8 (the running code and the paste operation). My conclusion is that somewhere deep in the system it's detecting the need for windows line endings and converting each of the \r and \n into \r\n just before the paste happens.
Upvotes: 4
Views: 354
Reputation: 958
I resolved this issue with a simple replaceAll like this:
final ClipboardContent content = new ClipboardContent();
content.putString(str.replaceAll("\r\n", "\n"));
Upvotes: 1