String repeat java语言
Write a function called repeatStr which repeats the given string string exactly n times.
repeatStr(6, “I”) // “IIIIII”
repeatStr(5, “Hello”) // “HelloHelloHelloHelloHello”
下面展示一些 内联代码片
。
public class Solution {
public static String repeatStr(final int repeat, final String string) {
int i= 0;
String repeatedStr = "";
for(i=0;i<repeat;i++){
repeatedStr = string + repeatedStr;
}
System.out.println(repeatedStr);
return repeatedStr;
}
}
public static Sring repeatStr(final int repeat,final String string) 是很好的方法用例
初始化空字符串
for循环
这里在引出一个字符串数组的创建方式:
String[] str = new String[5]; //创建一个长度为5的String(字符串)型的一维数组
String[] str = new String[]{"","","","",""};
String[] str = {"","","","",""};