static public String normalizeToEncoding(String origString_,Charset encoding_){
  String normString=origString_;
  CharsetEncoder encoder=encoding_.newEncoder();
  if (!encoder.canEncode(origString_)) {
    final int length=origString_.length();
    char[] normSeq=new char[(origString_.length())];
    int charNum=0;
    for (int offset=0; offset < length; ) {
      Pair<Character,Integer> replacement=normalizeCodepoint(origString_,encoding_,offset);
      Character replacedChar=replacement.getFirst();
      int codepoint=replacement.getSecond();
      if (null != replacedChar) {
        normSeq[charNum]=replacedChar;
        charNum++;
      }
      offset+=Character.charCount(codepoint);
    }
    normString=new String(normSeq);
  }
  return normString;
}

static public String normalizeToEncoding ( String origString_ , Charset encoding_ ) { String normString = origString_ ; CharsetEncoder encoder = encoding_ . newEncoder ( ) ; if ( ! encoder . canEncode ( origString_ ) ) { final int length = origString_ . length ( ) ; char [ ] normSeq = new char [ ( origString_ . length ( ) ) ] ; int charNum = 0 ; for ( int offset = 0 ; offset < length ; ) { Pair < Character , Integer > replacement = normalizeCodepoint ( origString_ , encoding_ , offset ) ; Character replacedChar = replacement . getFirst ( ) ; int codepoint = replacement . getSecond ( ) ; if ( null != replacedChar ) { normSeq [ charNum ] = replacedChar ; charNum ++ ; } offset += Character . charCount ( codepoint ) ; } normString = new String ( normSeq ) ; } return normString ; }
tries to normalize string to specified encoding . the number of characters returned should be the same , and tokens should remain contiguous in the output ; non - recognized characters will be substituted for * something * .
