Java 替换字符串中的辅音

Java 替换字符串中的辅音,java,string,replace,Java,String,Replace,我写下这个函数的代码有困难 替换辅音 /** Return a string that is s but with all lowercase consonants (letters of * the English alphabet other than the vowels a, e, i, o, u) replaced with * _, and all uppercase consonants replaced with ^. * * Examples: For s = "Min

我写下这个函数的代码有困难

替换辅音

/** Return a string that is s but with all lowercase consonants (letters of
 * the English alphabet other than the vowels a, e, i, o, u) replaced with
 * _, and all uppercase consonants replaced with ^.
 *
 * Examples: For s = "Minecraft" return "^i_e__a__".
 *           For s = "Alan Turing" return "A_a_ ^u_i__".
这就是我到目前为止所做的:

String consonants = "bcdfghjklmnpqrstvwxyz";
for(int j = 0; j < consonants.length(); j++ ){
    int start = 0;
    if s.charAt(start) == consonants( I am unsure to tell it to look through the string I made)
        s.charAt(start).replace(s.substring(start,1), ("_"));
        if s.charAt(start) == s.substring(start,start+1).ToUpperCase(){
            s.charAt(start) = "'";
        }
    }
}
String辅音=“bcdfghjklmnpqrstvwxyz”;
for(int j=0;j
您可以使用基于负前瞻的正则表达式,或者需要手动写入所有辅音

String s = "Minecraft";
String m = s.replaceAll("(?![aeiouAEIOU])[a-z]", "_").replaceAll("(?![aeiouAEIOU])[A-Z]", "^");
System.out.println(m);
输出:

^i_e__a__
如果这不是你想要的:

String myName = "domanokz";
String newName = myName.substring(0,4)+'x'+myName.substring(5);
也可以使用StringBuilder:

StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);

可能是“Braaains”的复制品?这是可行的,我还想知道如何使用for循环使其工作。如果s字符串是大写字母,是否必须有if语句?或者我会使用内置函数replace吗?您尝试过其他解决方案吗?OP要求将字符转换为u或^。你的例子都不是这样的。
StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);