Home > Enterprise >  How to repeat characters n times
How to repeat characters n times

Time:10-26

I've try to do it but it only repeat the characters twice

Tried this

Sting name = "has";


for (int i = 0; i < 3; i  ) {
  second  = name.substring (i, i 1)   name.substring(i,i 1);
}

CodePudding user response:

public String repeatEachN(String str, int n) {
int len = str.length();
StringBuffer sb = new StringBuffer();
for(int i=0;i<len;i  ) {
  for(int j=0;j<n;j  ) {
     sb.append(str.charAt(i));
  }
}
return sb.toString();
}

CodePudding user response:

From java 11 you can use repeat method to repeat string n times

public static String repeatTimes(String s, int n) {
    StringBuilder builder = new StringBuilder();
    for (char c : s.toCharArray()) {
        builder.append(String.valueOf(c).repeat(n));
    }
    return builder.toString();
}

CodePudding user response:

If I understand your problem in the right way, you want to pass in a string then multiply every char in the string n time while concat them.

String name = "has";
String second="";
int n=3;
for (int i = 0; i < name.length(); i  ) {
    for (int j = 0; j < n; j  ) {
        second =name.charAt(i);
    }
}

This code works in java, I advise you to use functions like below:

public String multiplyCharacters(String str, int multiplyCount){
String result="";
    for (int i = 0; i < str.length(); i  ) {
        for (int j = 0; j < multiplyCount; j  ) {
            result =str.charAt(i);
        }
    }
return result;
}

In a more optimal way you should use StringBuilder to avoid to create multiple strings in the string pool

public String multiplyCharacters(String str, int multiplyCount){
StringBuilder result=new StringBuilder();
    for (int i = 0; i < str.length(); i  ) {
        for (int j = 0; j < multiplyCount; j  ) {
            result.append(str.charAt(i));
        }
    }
return result.toString();
}

CodePudding user response:

Assuming that you're a beginner in java, you can use String concatenation with " =" (that would look like this).

    String name = "has";
    String second = "";

    for (int i = 0; i < 3; i  ) {
        for (int j = 0; j < n; j  ) {
            second  = name.charAt(i);
        }
    }

It would indeed be better practice to use something like a StringBuilder/Stringbuffer or String.repeat() as modern IDEs like IntelliJ automatically suggest.

    String name = "has";
    StringBuilder second = new StringBuilder();

    for (int i = 0; i < 3; i  ) {
       second.append(String.valueOf(name.charAt(i)).repeat(n));
    }
    String s = second.toString();

CodePudding user response:

"has".codePoints()
    .mapToObj(c -> Character.toString(c).repeat(n))
    .collect(Collectors.joining())

Result when n = 4: hhhhaaaassss

CodePudding user response:

        String name = "has";
        String second = "";
        
        for (int i = 0; i < name.length(); i  ) {
          for (int j = 0; j < 3; j  ) {
              second  = name.charAt(i);
          }
        }
        
        System.out.println(second);

CodePudding user response:

Try this.

static String repeatCharacter(String s, int n) {
    return s.codePoints()
        .mapToObj(ch -> Character.toString(ch).repeat(n))
        .collect(Collectors.joining());
}

public static void main(String[] args) {
    System.out.println(repeatCharacter("has", 5));
}

output:

hhhhhaaaaasssss

CodePudding user response:

for (int i = 0; i < 3; i  ) {
    for(int j=0;j<n;j  ) // the number of times you wish to iterate, replace n with 4 to get your "hhhhaaaassss"
    {
        second  = name.substring (i, i 1);
    }
}
  •  Tags:  
  • java
  • Related