【问题标题】:Doubling each letter in a String将字符串中的每个字母加倍
【发布时间】:2013-11-18 16:13:53
【问题描述】:

我正在为 Java 1 做一个项目,我完全被这个问题所困扰。

基本上我需要将字符串中的每个字母加倍。

"abc"  ->  "aabbcc"
"uk"   ->  "uukk"
"t"    ->  "tt"

我需要在一个被认为是“Java 1”价值的循环中进行。所以我猜这意味着更多的问题。

据我所知,我知道最简单的方法是在 while 循环中使用 charAt 方法,但由于某种原因,我无法弄清楚如何将字符返回到另一种方法一个字符串。

谢谢

[编辑] 我的代码(错误,但也许这会有所帮助)

int index = 0;
  int length = str.length();
  while (index < length) {
      return str.charAt(index) + str.charAt(index);
      index++;
  }

【问题讨论】:

  • 看看StringBuilder,看看是否对你有帮助。我会使用for 循环而不是while 循环。
  • 你写过代码来解决这个问题吗?
  • 认真的 Java 1?为什么?你从哪里得到的?
  • @RaviKumar,实际上是的。我相信我的“逻辑”是正确的,这是一种我似乎无法得到的实际方法。如果有帮助,我会在问题中添加它。
  • @NeplatnyUdaj 说真的 Java 1 是什么?这是一堂高中课。

标签: java while-loop char


【解决方案1】:
String s="mystring".replaceAll(".", "$0$0");

String.replaceAll 方法使用documentation of the Pattern class 中描述的正则表达式语法,我们可以在其中了解到. 匹配“任何字符”。在替换中,$number 指的是编号的“捕获组”,而 $0 被预定义为整个匹配项。所以$0$0 两次引用匹配的字符。正如该方法的名称所暗示的,它针对所有匹配项执行,即所有字符。

【讨论】:

    【解决方案2】:

    是的,for 循环在这里确实更有意义,但如果您需要使用 while 循环,那么它看起来像这样:

    String s = "abc";
    String result = "";
    int i = 0;
    while (i < s.length()){
        char c = s.charAt(i);
        result = result + c + c;
        i++;
    }
    

    【讨论】:

    • 正是我想要的!感谢您像我问的那样使用 while 循环!我完全忘记了您可以在循环期间更改变量的值:)
    【解决方案3】:

    你可以这样做:

    public void doubleString(String input) {
    
        String output = "";
    
        for (char c : input.toCharArray()) {
            output += c + c;
        }
    
        System.out.println(output);
    
    }
    

    【讨论】:

    • 如果您输入的是一个单词,例如“你好”,那么输出将是“208202216216222”。我猜这不是故意的。
    【解决方案4】:

    你的直觉很好。 charAt(i) 将返回字符串中位于i 位置的字符,是吗?

    您还说过您想使用循环。 for 循环,遍历列表的长度,string.length(),将允许您执行此操作。在字符串中的每个节点上,您需要做什么?将字符加倍。

    让我们看看你的代码:

    int index = 0;
    int length = str.length();
    while (index < length) {
        return str.charAt(index) + str.charAt(index);    //return ends the method
        index++;
    }
    

    您的代码有问题,您在进入循环后立即返回两个字符。因此,对于字符串abc,您将返回aa。让我们将aa 存储在内存中,然后像这样返回完整的字符串:

    int index = 0;
    int length = str.length();
    String newString = "";
    while (index < length) {
        newString += str.charAt(index) + str.charAt(index);
        index++;
    }
    return newString;
    

    这会将字符添加到newString,允许您返回整个完整的字符串,而不是一组双字符。

    顺便说一句,这可能更容易作为一个 for 循环来完成,可以压缩和澄清您的代码。我的个人解决方案(对于 Java 1 类)看起来像这样:

    String newString = "";
    for (int i = 0; i < str.length(); i++){
        newString += str.charAt(i) + str.charAt(i);
    }
    return newString;
    

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      试试这个

          String a = "abcd";
          char[] aa = new char[a.length() * 2];
          for(int i = 0, j = 0; j< a.length(); i+=2, j++){
              aa[i] = a.charAt(j);
              aa[i+1]= a.charAt(j);
          }
          System.out.println(aa);
      

      【讨论】:

        【解决方案6】:
        public static char[] doubleChars(final char[] input) {
            final char[] output = new char[input.length * 2];
            for (int i = 0; i < input.length; i++) {
                output[i] = input[i];
                output[i + 1] = input[i];
            }
            return output;
        }
        

        【讨论】:

          【解决方案7】:

          假设这是在一个方法中,你应该明白你只能从一个方法返回一次。遇到return语句后,控件返回到调用方法。因此,您每次在循环中返回 char 的方法是错误的。

          int index = 0;
          int length = str.length();
          while (index < length) {
             return str.charAt(index) + str.charAt(index); // only the first return is reachable,other are not executed
             index++;
           }
          

          改变你的方法来构建一个字符串并返回它

          public String modify(String str)
          {
              int index = 0;
              int length = str.length();
              String result="";
              while (index < length) {
                 result += str.charAt[index]+str.charAt[index];
                 index++;
              }
              return result;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-04
            • 1970-01-01
            • 2020-12-31
            相关资源
            最近更新 更多