cn.hutool.core.text.finder.StrFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.core.text.finder;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
/**
* 字符串查找器
*
* @author looly
* @since 5.7.14
*/
public class StrFinder extends TextFinder {
private static final long serialVersionUID = 1L;
private final CharSequence strToFind;
private final boolean caseInsensitive;
/**
* 构造
*
* @param strToFind 被查找的字符串
* @param caseInsensitive 是否忽略大小写
*/
public StrFinder(CharSequence strToFind, boolean caseInsensitive) {
Assert.notEmpty(strToFind);
this.strToFind = strToFind;
this.caseInsensitive = caseInsensitive;
}
@Override
public int start(int from) {
Assert.notNull(this.text, "Text to find must be not null!");
final int subLen = strToFind.length();
if (from < 0) {
from = 0;
}
int endLimit = getValidEndIndex();
if (negative) {
for (int i = from; i > endLimit; i--) {
if (CharSequenceUtil.isSubEquals(text, i, strToFind, 0, subLen, caseInsensitive)) {
return i;
}
}
} else {
endLimit = endLimit - subLen + 1;
for (int i = from; i < endLimit; i++) {
if (CharSequenceUtil.isSubEquals(text, i, strToFind, 0, subLen, caseInsensitive)) {
return i;
}
}
}
return INDEX_NOT_FOUND;
}
@Override
public int end(int start) {
if (start < 0) {
return -1;
}
return start + strToFind.length();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy