You'll need to set an array where the things you want to replace are, and then another with the replacements.
Since your replacement matches many chars, it would be better for you to have a matrix.
Once you know what you want, you build an algorithm.
to_replace: matrix
replacements: array
data: char array
result: char array
for each letter in data
char replacement = findReplacementFor( letter ).in(matrix)
result[i++] = replacement
end
Or something like that, the point is to describe what steps you need to perform to accomplish what you want.
And then implement that algorithm.
Here's the running code.
public class Conv {
static char [][] toReplace = {
{'a','á','à', 'â', 'ä'},
{'b'},
{'c'},
{'d'},
{'e', 'é', 'è', 'ê', 'ë'}
// complete with the remaining as needed.
};
static char [] replacement = {
'A','B','C','D','E'
};
public static void main( String [] args ) {
char [] converted = convert("Convërt mê. à".toCharArray());
System.out.println( new String( converted ));
}
/**
*Transform the given char array into it's equivalent according to the conversion table.
*/
private static char [] convert( char [] data ) {
char [] result = new char[data.length];
for( int i = 0 ; i < data.length ; i++ ){
result[i] = findReplacement( data[i] );
}
return result;
}
/**
* Find a replacement for the given char, if there is no a suitable replacement
* return the same char
*/
private static char findReplacement( char c ) {
int i = indexOfReplacementFor( c );
if( i >= 0 ) {
return replacement[i];
}
return c;
}
/**
* Search in the reaplcement table what is the corresponding index to replace
*/
private static int indexOfReplacementFor( char c ) {
for( int i = 0 ; i < toReplace.length ; i++ ) {
for( int j = 0 ; j < toReplace[i].length; j++ ){
if( c == toReplace[i][j] ){
return i;
}
}
}
return -1;
}
}
Notice that I have splited the function into smaller ones for clarity. I hope the code is easy enough for you to grasp it.
The missing part it how to write accentuated char literals in java, but I'm pretty sure that could be asked here in SO.
EDIT My compiler ( javac from OSX distribution ) wasn't using the UT8 encoding for me.
I'm posting the right code and it works by following this accepted answer.