public static String asJavaIdentifier(String s){
  if (s.isEmpty())   return "$0";
  StringBuilder b=new StringBuilder();
  if (!s.startsWith("$")) {
    b.append("$");
    for (    char c : s.toCharArray())     if (Character.isJavaIdentifierPart(c))     b.append(c);
 else     b.append("_");
  }
 else {
    b.append(s);
  }
  return b.toString();
}

public static String asJavaIdentifier ( String s ) { if ( s . isEmpty ( ) ) return "$0" ; StringBuilder b = new StringBuilder ( ) ; if ( ! s . startsWith ( "$" ) ) { b . append ( "$" ) ; for ( char c : s . toCharArray ( ) ) if ( Character . isJavaIdentifierPart ( c ) ) b . append ( c ) ; else b . append ( "_" ) ; } else { b . append ( s ) ; } return b . toString ( ) ; }
converts strings to valid java identifiers by replacing illegal characters with underscores .
