Note that there are some explanatory texts on larger screens.

plurals
  1. POHard time with escape character
    primarykey
    data
    text
    <p>I need to strip out a few invalid characters from a string and wrote the following code part of a StringUtil library:</p> <pre><code>public static String removeBlockedCharacters(String data) { if (data==null) { return data; } return data.replaceAll("(?i)[&lt;|&gt;|\u003C|\u003E]", ""); } </code></pre> <p>I have a test file illegalCharacter.txt with one line in it:</p> <pre><code>hello \u003c here &lt; and &gt; there </code></pre> <p>I run the following unit test:</p> <pre><code>@Test public void testBlockedCharactersRemoval() throws IOException{ checkEquals(StringUtil.removeBlockedCharacters("a &lt; b &gt; c\u003e\u003E\u003c\u003C"), "a b c"); log.info("Procesing from string directly: " + StringUtil.removeBlockedCharacters("hello \u003c here &lt; and &gt; there")); log.info("Procesing from file to string: " + StringUtil.removeBlockedCharacters(FileUtils.readFileToString(new File("src/test/resources/illegalCharacters.txt")))); } </code></pre> <p>I get:</p> <pre><code>INFO - 2010-09-14 13:37:36,111 - TestStringUtil.testBlockedCharactersRemoval(36) | Procesing from string directly: hello here and there INFO - 2010-09-14 13:37:36,126 - TestStringUtil.testBlockedCharactersRemoval(37) | Procesing from file to string: hello \u003c here and there </code></pre> <p>I am VERY confused: as you can see, the code properly strips out the '&lt;', '>', and '\u003c' if I pass a string containing these values but it fails to strip out '\u003c' if I read from a file containing the same string.</p> <p>My questions, so that I stop loosing hair over it, are:</p> <ol> <li>Why do I get this behavior?</li> <li>How can I change my code to properly strip \u003c in all occasions?</li> </ol> <p>Thanks</p>
    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. 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