![](/img/trans.png)
[英]Return the number of times that the string “hello” appears anywhere in the given string
[英]Adding a given String to itself a number of given times
在一個任意的類中,我決定根據所需的分隔符和所需的數量來制作分隔符的方法。
public String divider(String s, int amount){
StringBuilder repeated = new StringBuilder();
for(int i = 0; i < amount; i++){repeated.append(s);}
return repeated.toString();
}
該方法在Catalog
類中定義。 我稱它為
this.divider("-", 10);
在類的toString()
中。 我在main方法中mainCat.divider("-", 10)
mainCat是一個Catalog對象。
我嘗試了多種不同的方式來將一個字符串自身乘以一個循環來復制自己,但是它們對我來說都是空白。 任何建議將不勝感激,謝謝!
您最好只使用StringUtils.repeat(str, count)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.