use of cn.hutool.core.text.StrBuilder in project hutool by looly.
the class StrUtil method move.
/**
* 循环位移指定位置的字符串为指定距离<br>
* 当moveLength大于0向右位移,小于0向左位移,0不位移<br>
* 当moveLength大于字符串长度时采取循环位移策略,既位移到头后从头(尾)位移,例如长度为10,位移13则表示位移3
*
* @param str 字符串
* @param startInclude 起始位置(包括)
* @param endExclude 结束位置(不包括)
* @param moveLength 移动距离,负数表示左移,正数为右移
* @return 位移后的字符串
* @since 4.0.7
*/
public static String move(CharSequence str, int startInclude, int endExclude, int moveLength) {
if (isEmpty(str)) {
return str(str);
}
int len = str.length();
if (Math.abs(moveLength) > len) {
// 循环位移,当越界时循环
moveLength = moveLength % len;
}
final StrBuilder strBuilder = StrBuilder.create(len);
if (moveLength > 0) {
int endAfterMove = Math.min(endExclude + moveLength, str.length());
//
strBuilder.append(str.subSequence(0, startInclude)).append(//
str.subSequence(endExclude, endAfterMove)).append(//
str.subSequence(startInclude, endExclude)).append(str.subSequence(endAfterMove, str.length()));
} else if (moveLength < 0) {
int startAfterMove = Math.max(startInclude + moveLength, 0);
//
strBuilder.append(str.subSequence(0, startAfterMove)).append(//
str.subSequence(startInclude, endExclude)).append(//
str.subSequence(startAfterMove, startInclude)).append(str.subSequence(endExclude, str.length()));
} else {
return str(str);
}
return strBuilder.toString();
}
use of cn.hutool.core.text.StrBuilder in project hutool by looly.
the class StrUtil method repeatAndJoin.
/**
* 重复某个字符串并通过分界符连接
*
* <pre>
* StrUtil.repeatAndJoin("?", 5, ",") = "?,?,?,?,?"
* StrUtil.repeatAndJoin("?", 0, ",") = ""
* StrUtil.repeatAndJoin("?", 5, null) = "?????"
* </pre>
*
* @param str 被重复的字符串
* @param count 数量
* @param conjunction 分界符
* @return 连接后的字符串
* @since 4.0.1
*/
public static String repeatAndJoin(CharSequence str, int count, CharSequence conjunction) {
if (count <= 0) {
return EMPTY;
}
final StrBuilder builder = StrBuilder.create();
boolean isFirst = true;
while (count-- > 0) {
if (isFirst) {
isFirst = false;
} else if (isNotEmpty(conjunction)) {
builder.append(conjunction);
}
builder.append(str);
}
return builder.toString();
}
use of cn.hutool.core.text.StrBuilder in project hutool by looly.
the class StrUtil method replace.
/**
* 替换字符串中的指定字符串
*
* @param str 字符串
* @param fromIndex 开始位置
* @param searchStr 被查找的字符串
* @param replacement 被替换的字符串
* @param ignoreCase 是否忽略大小写
* @return 替换后的字符串
* @since 4.0.3
*/
public static String replace(CharSequence str, int fromIndex, CharSequence searchStr, CharSequence replacement, boolean ignoreCase) {
if (isEmpty(str) || isEmpty(searchStr)) {
return str(str);
}
if (null == replacement) {
replacement = EMPTY;
}
final int strLength = str.length();
final int searchStrLength = searchStr.length();
if (fromIndex > strLength) {
return str(str);
} else if (fromIndex < 0) {
fromIndex = 0;
}
final StrBuilder result = StrBuilder.create(strLength + 16);
if (0 != fromIndex) {
result.append(str.subSequence(0, fromIndex));
}
int preIndex = fromIndex;
int index = fromIndex;
while ((index = indexOf(str, searchStr, preIndex, ignoreCase)) > 0) {
result.append(str.subSequence(preIndex, index));
result.append(replacement);
preIndex = index + searchStrLength;
}
if (preIndex < strLength) {
// 结尾部分
result.append(str.subSequence(preIndex, strLength));
}
return result.toString();
}
Aggregations