String (String text, final int mnemonic, final int mnemonicIndex) { if (text == null) { text = ""; } final int index; if( mnemonicIndex >= 0 && mnemonicIndex < text.length() && Character.toUpperCase(text.charAt(mnemonicIndex)) == mnemonic ){ // Index really corresponds to the mnemonic index = mnemonicIndex; } else{ // Mnemonic exists but index is wrong index = -1; } final StringBuilder buffer = new StringBuilder(text); if(index != -1){ buffer.insert(index, '&'); // Quote all '&' except inserted one for(int i = buffer.length() - 1; i >= 0; i--){ if(buffer.charAt(i) == '&' && i != index){ buffer.insert(i, '&'); } } } return buffer.toString(); }