Quotes '\' and '$' in s, so that the returned string could be used in #appendReplacement as a literal replacement of s. @param s the string to be quoted @return the quoted string
(String s)
| 406 | * @return the quoted string |
| 407 | */ |
| 408 | public static String quoteReplacement(String s) { |
| 409 | if (s.indexOf('\\') < 0 && s.indexOf('$') < 0) { |
| 410 | return s; |
| 411 | } |
| 412 | StringBuilder sb = new StringBuilder(); |
| 413 | for (int i = 0; i < s.length(); ++i) { |
| 414 | char c = s.charAt(i); |
| 415 | if (c == '\\' || c == '$') { |
| 416 | sb.append('\\'); |
| 417 | } |
| 418 | sb.append(c); |
| 419 | } |
| 420 | return sb.toString(); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Appends to {@code sb} two strings: the text from the append position up to the beginning of the |