【问题标题】:Adding some string before specific characters in Java在Java中的特定字符之前添加一些字符串
【发布时间】:2017-07-26 16:08:41
【问题描述】:

我想在每个元音之前添加“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;
}

【问题讨论】:

  • 你也可以只使用input.replaceAll("A", "OBA").replaceAll("E", "OBE")等等,或者直接使用正则表达式input.replaceAll("([AEIOU])", "OB$1")。另外,您当前解决方案的输出是什么?
  • 在什么情况下它不起作用?它是否因错误而失败?如果是这样,请向我们展示堆栈跟踪。输出不正确吗?如果是这样,请告诉我们你得到了什么。
  • replace 方法替换其第一个参数的所有货币。

标签: java string loops character


【解决方案1】:

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

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

【讨论】:

  • 虽然这解决了 OP 试图解决的任务,但它并没有解释为什么他的方法没有按预期工作。
  • 另外,这不会删除OP打算保留的元音吗?
  • @Jason $1 是匹配的元音
  • 完美,这也是我要回答的问题。
  • 谢谢伙计。虽然我不明白,因为我还是新手。但我稍后会回复它
【解决方案2】:

你应该替换

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

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

您的问题是 replace 对其第一个参数的所有出现都起作用。

当您有“THobIS IS A TEST”并尝试替换标记的字母时,您会替换两个“I”字母。在它之后,您在第二个“I”之前索引指向完全错误的位置。迟早你会再次遇到它,情况会重复。

【讨论】:

    【解决方案3】:

    您的问题出在replace 电话中。 Documentaion 告诉它用新的子字符串替换每个子字符串。 所以你的字符串无限增长: 第一次替换后是:“THobIS obIS A TEST” 下一个之后是:“THobobIS obobIS A TEST” 然后“THobobobIS obobobIS 测试” 等等……

    如果你换行

    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 < test.length() 并摆脱第二个 if。

    【讨论】:

      【解决方案4】:

      我认为这样更容易:对于“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;
      }
      

      【讨论】:

      • 欢迎来到 SO 并感谢您的回答。请正确格式化和缩进您的代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2022-11-10
      • 2014-05-22
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      相关资源
      最近更新 更多