如何创建在每个元音后添加破折号的方法?

static char[] vowelArray = {'e', 'i', 'u', 'o', 'a'};


    public static String insertDashAfterWovel(String input){


    char[] tmp=input.toCharArray();


    for (int i = 0; i < tmp.length; i++) {

        if (tmp[i]== vowelArray[i]){

            tmp[i]+='-';

            return tmp.toString();

        }

    }

    return "";

我做的方法不行。如何使它在每个检测到的元音后添加一个破折号并返回如下示例中的字符串?我为这个方法做了一个测试类来验证我是否正确。


“角斗士”应该返回“gla-di-a-to-r”


摇曳的蔷薇
浏览 142回答 3
3回答

守着星空守着你

您可以像这样使用正则表达式:String str = "gladiator";&nbsp; // your input stringstr = str.replaceAll("([aeiou])", "$1-");System.out.println(str);

慕尼黑5688855

尝试这个:&nbsp;static char[] vowelArray = {'e', 'i', 'u', 'o', 'a'};&nbsp;public static String insertDashAfterWovel(String input){&nbsp; &nbsp; char[] tmp=input.toCharArray();&nbsp; &nbsp; for (int i = 0; i < vowelArray.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<tmp.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (vowelArray[i]==tmp[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input=input.replace(input.substring(j,j+1),(input.substring(j,j+1)+"-"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return input;&nbsp;}

FFIVE

由于海报的问题是:我做的方法不行。如何使它在每个检测到的元音后添加一个破折号并返回如下示例中的字符串?“角斗士”应该返回“gla-di-a-to-r”我提供了一个与原始方法最相似的工作示例。static char[] vowelArray = {'e', 'i', 'u', 'o', 'a'};public static String insertDashAfterVowel(String input){&nbsp; &nbsp; char[] tmp=input.toCharArray();&nbsp; &nbsp; String newString = "";&nbsp; &nbsp; for (int i = 0; i < tmp.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; newString += tmp[i];&nbsp; &nbsp; &nbsp; &nbsp; if (new String(vowelArray).indexOf(tmp[i]) != -1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newString += "-";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return newString;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java