java 连接 重复 字符串 简单方法是_在Java中重复字符串的简单方法

/**

* Helper-Class for Repeating Strings and other CharSequence-Implementations

* @author Maciej Schuttkowski

*/publicclassRepeatingCharSequenceimplementsCharSequence{finalintcount;CharSequenceinternalCharSeq="";CharSequenceseparator="";/**

* CONSTRUCTOR - RepeatingCharSequence

* @param input CharSequence to repeat

* @param count Repeat-Count

*/publicRepeatingCharSequence(CharSequenceinput,intcount){if(count<0)thrownewIllegalArgumentException("Can not repeat String \""+input+"\" less than 0 times! count="+count);if(count>0)internalCharSeq=input;this.count=count;}/**

* CONSTRUCTOR - Strings.RepeatingCharSequence

* @param input CharSequence to repeat

* @param count Repeat-Count

* @param separator Separator-Sequence to use

*/publicRepeatingCharSequence(CharSequenceinput,intcount,CharSequenceseparator){this(input,count);this.separator=separator;}@OverridepublicCharSequencesubSequence(intstart,intend){checkBounds(start);checkBounds(end);intsubLen=end-start;if(subLen<0){thrownewIndexOutOfBoundsException("Illegal subSequence-Length: "+subLen);}return(start==0&&end==length())?this:toString().substring(start,subLen);}@Overridepublicintlength(){//We return the total length of our CharSequences with the separator 1 time less than amount of repeats:returncount<1?0:((internalCharSeq.length()*count)+(separator.length()*(count-1)));}@OverridepubliccharcharAt(intindex){finalintinternalIndex=internalIndex(index);//Delegate to Separator-CharSequence or Input-CharSequence depending on internal index:if(internalIndex>internalCharSeq.length()-1){returnseparator.charAt(internalIndex-internalCharSeq.length());}returninternalCharSeq.charAt(internalIndex);}@OverridepublicStringtoString(){returncount<1?"":newStringBuilder(this).toString();}privatevoidcheckBounds(intindex){if(index<0||index>=length())thrownewIndexOutOfBoundsException("Index out of Bounds: "+index);}privateintinternalIndex(intindex){// We need to add 1 Separator-Length to total length before dividing,// as we subtracted one Separator-Length in "length()"returnindex%((length()+separator.length())/count);}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值