Java String replace Microsoft smart quotes curly " and '
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = "abc\u0091demo2s.com\u0091\u0091"; System.out.println(replaceSmartQuotes(str)); }// w w w . j a va2s. c o m /** * Replaces microsoft "smart quotes" (curly " and ') with their * ascii counterparts. */ public static String replaceSmartQuotes(String str) { // See http://www.microsoft.com/typography/unicode/1252.htm str = replaceChars(str, "\u0091\u0092\u2018\u2019", '\''); str = replaceChars(str, "\u0093\u0094\u201c\u201d", '"'); return str; } /** * Like String.replace() except that it accepts any number of old chars. * Replaces any occurrances of 'oldchars' in 'str' with 'newchar'. * Example: replaceChars("Hello, world!", "H,!", ' ') returns " ello world " */ public static String replaceChars(String str, String oldchars, char newchar) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(str); do { buf.setCharAt(pos, newchar); pos = indexOfChars(str, oldchars, pos + 1); } while (pos != -1); return buf.toString(); } /** * Like String.indexOf() except that it will look for any of the * characters in 'chars' (similar to C's strpbrk) */ public static int indexOfChars(String str, String chars) { return indexOfChars(str, chars, 0); } /** * Like String.indexOf() except that it will look for any of the * characters in 'chars' (similar to C's strpbrk) */ public static int indexOfChars(String str, String chars, int fromIndex) { final int len = str.length(); for (int pos = fromIndex; pos < len; pos++) { if (chars.indexOf(str.charAt(pos)) >= 0) { return pos; } } return -1; } }