Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,在特定字符之前添加一些字符串_Java_String_Loops_Character - Fatal编程技术网

在Java中,在特定字符之前添加一些字符串

在Java中,在特定字符之前添加一些字符串,java,string,loops,character,Java,String,Loops,Character,我想在每个元音前加上“OB”。 示例输入:“这是一个测试” 样本输出:“THOBIS OBIS OBA TOBEST” 我不知道为什么我的代码不起作用: public static String obify(String test) { int x = 0; while (x != -1) { if (test.charAt(x) == 'A' || test.charAt(x) == 'E' || test.charAt(x) == 'I' || test.ch

我想在每个元音前加上“OB”。 示例输入:“这是一个测试” 样本输出:“THOBIS OBIS OBA TOBEST” 我不知道为什么我的代码不起作用:

public static String obify(String test) {
    int x = 0;
    while (x != -1) {
        if (test.charAt(x) == 'A' || test.charAt(x) == 'E' || test.charAt(x) == 'I' || test.charAt(x) == 'O' || test.charAt(x) == 'U') {
            test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));
            x += 3;
        } else {
            x++;
        }
        if (x >= test.length() - 1) {
            x = -1;
        }
    }
    return test;
}

一个简单正则表达式的完美场景

String foo = "hEllo what's up?";
String rep = foo.replaceAll("(?i)([aeiou])", "OB$1");
System.out.println(rep);
你应该替换

test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));

您的问题是
replace
对其第一个参数的所有匹配项进行操作


当您有“THobISIS A TEST”并尝试替换标记的字母时,您将替换两个“I”字母。在它之后,在第二个“I”之前,你的索引指向完全错误的位置。迟早你会重蹈覆辙的。

你的问题在于
替换
呼叫。
文档说明它用新的子字符串替换每个子字符串。
因此,您的字符串将无限增长:
第一次替换后是:“THobIS obIS是一个测试”
下一个是:“Thobobbis obobIS A TEST”
然后是“测试”
等等

如果你改变路线

test=test.replace(test.substring(x,x+1),“ob”+test.substring(x,x+1))

test=test.substring(0,x)+“ob”+test.substring(x)

它将完成这项工作


您还可以将while条件更改为
x
,并去掉第二个if。

我认为这样更容易:对于单词“GTREIS”: 我基本上取元音前后的内容(包括元音),在第一部分加上“OB”,再加上剩余的,最后用修改过的字符串替换原来的字符串

public static String  obify(String s)
{ 
    String inloc ="OB",aux="";
    String start="";
    int n=s.length();
    int i=0;

    while (i<n)
    {
        if(s.charAt(i)=='A'|| s.charAt(i)=='E' || s.charAt(i)=='I' ||     s.charAt(i)=='O'||s.charAt(i)=='U' ){
            inloc ="OB";
            start="";
            aux=s.substring(i);//EIS
            System.out.println(aux);
            start=s.substring(0,i);//GTR
            System.out.println(start);
            start=start+inloc;//GTROB
            System.out.println(start);
            start=start+aux;
            s=start;
            i+=3;// here you have to jump over OBE for example and search the next vowel 
            n+=2;
        } else {
            i++;
        }
    }
    return s;
}
公共静态字符串obify(字符串s)
{ 
字符串inloc=“OB”,aux=“”;
字符串start=“”;
int n=s.长度();
int i=0;

而(iYou也可以只使用
input.replaceAll(“A”、“OBA”).replaceAll(“E”、“OBE”)等,或者直接使用regex
input.replaceAll(([AEIOU]),“OB$1”)
。另外,您当前解决方案的输出是什么?它以什么方式不工作?它是否因错误而失败?如果是,请向我们显示stacktrace。输出是否不正确?如果是,请向我们显示您得到的结果。
replace
方法替换其第一个参数的所有货币。虽然这解决了OP试图解决的任务,但它没有解释其原因这个方法没有达到预期的效果。另外,这会不会删除OP打算保留的元音?@Jason$1是匹配的元音完美,这也是我要回答的。谢谢,伙计。虽然我不明白,因为我还是个新手。但我会稍后再讨论欢迎来到SO,谢谢你的回答。请正确回答格式化并缩进代码
public static String  obify(String s)
{ 
    String inloc ="OB",aux="";
    String start="";
    int n=s.length();
    int i=0;

    while (i<n)
    {
        if(s.charAt(i)=='A'|| s.charAt(i)=='E' || s.charAt(i)=='I' ||     s.charAt(i)=='O'||s.charAt(i)=='U' ){
            inloc ="OB";
            start="";
            aux=s.substring(i);//EIS
            System.out.println(aux);
            start=s.substring(0,i);//GTR
            System.out.println(start);
            start=start+inloc;//GTROB
            System.out.println(start);
            start=start+aux;
            s=start;
            i+=3;// here you have to jump over OBE for example and search the next vowel 
            n+=2;
        } else {
            i++;
        }
    }
    return s;
}