title: How do I perform an encoding-independent string-comparison in Java?
body: <p>I'm having a strange problem comparing strings. I send a string to my server (as bytes using <code>getBytes()</code>) from the client. I've ensured that encoding is the same on the client and server by starting both of them with <code>-Dfile.encoding=UTF-8</code>.</p>
  
  <p>I noticed the problem when I was trying to perform a <code>valueOf</code> on the string I receive from the client, to convert it into an enum. When I print out the strings, they look <em>exactly</em> the same. But when I perform a <code>compareTo</code>, I get a non-zero number and <code>equals</code> returns <code>false</code>.</p>
  
  <p>I'm assuming that it is an encoding problem. I'm not really sure though -- I'm still a bit of a novice when it comes to client-server programming with sockets.</p>
  
  <p>This is what I get:</p>
  
  <pre><code>Waiting for connections on port 9090
  Connected to client: 127.0.0.1
  received command: GetAllItems
  The value is |GetAllItems| (from client)
  The value is |GetAllItems| (from enum)
  equals: false
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is how I'm reconstituting the string from the stream. Perhaps this is where I'm doing something wrong?</p>
  
  <pre><code>byte[] commandBytes = new byte[1024];
  in.read(commandBytes); //in is a BufferedInputReader
  String command = new String(commandBytes);
  </code></pre>