-1

I have string

String str="<td>"

And my input count=5

Is their any way in which I can get string equal to the counter

output string would be str="<td><td><td><td><td>"

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77

3 Answers3

4
String str = "";
for (int i = 0; i < count; i++) {
    str = str + "<td>";
}
System.out.println(str);
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1

Create a string as a new character array contraining count number of characters. The default character is the null character \0. Simply replace all null characters with your required String.

String str = new String(new char[count]).replace("\0", "<td>");
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
1

You can manually create it using StringBuilder

String str="<td>"
StringBuilder builder = new StringBuilder();
for(int i=0; i<count; i++){
   builder.append(str);
}

System.out.println(builder.toString());
dkatzel
  • 31,188
  • 3
  • 63
  • 67