dsantrat dsantrat -4 years ago 98
Java Question

Comparing clipboard contents to a string with a "\n" in it -Java

I'm reading in a txt file and then turning it into one string.
I'm having troubles checking clipboard contents against a string that contains a "\n". If the clipboard content has the line break in it it doesn't check as a match to the contents of the string. Say I copy the following:
This isn't equating to the same content already in the string. Code as follows.
Is it possible to get this to check correctly? If so how? If not is there a way I can check the text file while still assuring I keep the formatting as the \n is needed to keep the formatting for use later on.

BufferedReader clipboardStorageReader = new BufferedReader(clipboardStorageFile);
String clipboardStorageResponses = new String();

try {
for (String line; (line = clipboardStorageReader.readLine()) != null;){
clipboardStorageResponses += line;
}else {
clipboardStorageResponses += line + "\n";
} catch (IOException e1) {
// TODO Auto-generated catch block
if (clipboardStorageResponses.contains(result)) {


I'm making a program that holds onto at least the last 50 things I've coppied for use later on. Displaying this is a JScrollPane so I can clip a GUI to put it back on the clipboard. Just trying to avoid any duplicates.

Answer Source

you can remove the newline character by

line = line.trim(); // returns a copy of the string, with leading and trailing whitespace omitted. 

Alternatively you can replace it with empty string:

line = line.replace("\n", "").replace("\r", "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download