StringUtils { public static String quoteMetacharacters(String str) { if (str == null) { return null; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (UNSAFE_CHARS.contains(c)) { sb.append(ESCAPE_CHAR); } sb.append(c); } return sb.toString(); }  static int countWords(String str, boolean all); static String join(String[] array, String delimiter); static String quoteMetacharacters(String str); static String stripNasties(String str);  }
@Test public void testQuoteMetacharacters() { assertThat(StringUtils.quoteMetacharacters("foo"), is(equalTo("foo"))); assertThat(StringUtils.quoteMetacharacters("foo bar"), is(equalTo("foo bar"))); assertThat(StringUtils.quoteMetacharacters("\\"), is(equalTo("\\\\"))); assertThat(StringUtils.quoteMetacharacters("."), is(equalTo("\\."))); assertThat(StringUtils.quoteMetacharacters("+"), is(equalTo("\\+"))); assertThat(StringUtils.quoteMetacharacters("*"), is(equalTo("\\*"))); assertThat(StringUtils.quoteMetacharacters("?"), is(equalTo("\\?"))); assertThat(StringUtils.quoteMetacharacters("["), is(equalTo("\\["))); assertThat(StringUtils.quoteMetacharacters("^"), is(equalTo("\\^"))); assertThat(StringUtils.quoteMetacharacters("]"), is(equalTo("\\]"))); assertThat(StringUtils.quoteMetacharacters("$"), is(equalTo("\\$"))); assertThat(StringUtils.quoteMetacharacters("("), is(equalTo("\\("))); assertThat(StringUtils.quoteMetacharacters(")"), is(equalTo("\\)"))); assertThat(StringUtils.quoteMetacharacters("{"), is(equalTo("\\{"))); assertThat(StringUtils.quoteMetacharacters("}"), is(equalTo("\\}"))); assertThat(StringUtils.quoteMetacharacters("="), is(equalTo("\\="))); assertThat(StringUtils.quoteMetacharacters("!"), is(equalTo("\\!"))); assertThat(StringUtils.quoteMetacharacters("<"), is(equalTo("\\<"))); assertThat(StringUtils.quoteMetacharacters(">"), is(equalTo("\\>"))); assertThat(StringUtils.quoteMetacharacters("|"), is(equalTo("\\|"))); assertThat(StringUtils.quoteMetacharacters(":"), is(equalTo("\\:"))); assertThat(StringUtils.quoteMetacharacters("foo bar \\.+*?[^]$(){}=!<>|:"), is(equalTo("foo bar \\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:"))); assertThat(StringUtils.quoteMetacharacters(""), is(equalTo(""))); assertThat(StringUtils.quoteMetacharacters(null), is(equalTo(null))); }