Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a couple things:</p> <ul> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isSurrogate%28char%29"><code>Character.isSurrogate(char c)</code></a>:</p> <blockquote> <p>A char value is a surrogate code unit if and only if it is either a low-surrogate code unit or a high-surrogate code unit.</p> </blockquote></li> <li><p>Checking for pairs seems pointless, why not just remove all surrogates?</p></li> <li><p><code>x == false</code> is equivalent to <code>!x</code></p></li> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html"><code>StringBuilder</code></a> is better in cases where you don't need synchronization (like a variable that never leaves local scope).</p></li> </ul> <p>I suggest this:</p> <pre><code>public static String removeSurrogates(String query) { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; query.length(); i++) { char c = query.charAt(i); // !isSurrogate(c) in Java 7 if (!(Character.isHighSurrogate(c) || Character.isLowSurrogate(c))) { sb.append(firstChar); } } return sb.toString(); } </code></pre> <h3>Breaking down the <code>if</code> statement</h3> <p>You asked about this statement:</p> <pre><code>if (!(Character.isHighSurrogate(c) || Character.isLowSurrogate(c))) { sb.append(firstChar); } </code></pre> <p>One way to understand it is to break each operation into its own function, so you can see that the combination does what you'd expect:</p> <pre><code>static boolean isSurrogate(char c) { return Character.isHighSurrogate(c) || Character.isLowSurrogate(c); } static boolean isNotSurrogate(char c) { return !isSurrogate(c); } ... if (isNotSurrogate(c)) { sb.append(firstChar); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload