【问题标题】:repeating specific characters in string java在字符串java中重复特定字符
【发布时间】:2017-08-13 13:30:56
【问题描述】:

我有一个问题。我正在努力进行一项练习,要求我从用户那里获取一个字符串、一个我想在这个字符串中复制的字符和一个数字——我想复制多少次。 eg:字符串输入:dog;字符:o;数量:4。输出:doooog。我的问题是我怎样才能达到这个结果?

Scanner sc = new Scanner(System.in);

System.out.println("enter your string");
String text = sc.nextLine();

System.out.println("enter the character that will be repeated");
char character= sc.next().charAt(0);

System.out.println("enter the number of repetitions");
int repetitions = sc.nextInt();

for (int i = 0; i < text.length(); i++) {
    char z = text.charAt(i);
    if(z==character) {
         // repeat character in string put by user * repetitions 
    }
}
System.out.println(text);

【问题讨论】:

    标签: java string character repeat


    【解决方案1】:

    如果您使用的是 Java 8,则可以使用 String.join 并像这样替换:

    String str = "dog";
    int length = 4;
    String s = "o";
    str = str.replace(s, String.join("", Collections.nCopies(length, s)));// doooog
    

    阅读更多关于Collections::nCopies

    【讨论】:

      【解决方案2】:

      要完成您的代码,请插入以下内容:

      String ans="";
      for (int i = 0; i < text.length(); i++) {
          char z = text.charAt(i);
          if(z==character) {
               for(int j=0;j<repetitions;j++)
                  ans+=z;
          }
          else
              ans+=z;
      }
      

      但是,有一种更优雅的方法:

      String replacement="";
      for(int i=0;i<repetitions;i++)
          replacement+=character;
      String ans = text.replaceAll(String.valueOf(character), replacement);
      

      【讨论】:

        【解决方案3】:

        以下应该可以工作:

        StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < text.length(); i++) {
                            char z = text.charAt(i);
                            if(z==character) {
                                 // repeat character in string put by user * repetitions 
                                for (int j=1;j<=repetitions;j++){
                                    buffer.append(z);
                                }
                            }else{
                                 buffer.append(z);
                            }
                        }
        System.out.println(buffer.toString());
        

        【讨论】:

          【解决方案4】:

          如果你没有使用Java 8,你可以试试这个。

          StringBuilder builder = new StringBuilder();
          String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
          for (int i = 0; i < text.length(); i++) {
              char z = text.charAt(i);
              builder.append(z == character ? repetitionString : z);
          }
          
          text = builder.toString();
          System.out.println(text);
          

          在这个解决方案中,我们使用String.format 来重复我们想要的字符而不是循环。

          String.format("%" + repetitions + "s", z).replace(' ', z)
          

          这类似于我们可以在Python 中使用* 操作符,即

          z * repeatitions
          

          这将在z 中重复该字符所需的次数。

          另一种避免循环的方法是使用String.replace() 方法。

          String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
          text = text.replace(String.valueOf(character), repetitionString);
          System.out.println(text);
          

          【讨论】:

            猜你喜欢
            • 2016-02-28
            • 1970-01-01
            • 1970-01-01
            • 2017-03-02
            • 2012-10-21
            • 2023-03-23
            • 1970-01-01
            • 1970-01-01
            • 2016-08-30
            相关资源
            最近更新 更多