您的位置:首页 > 编程语言 > Java开发

(十九)Java工具类StringUtils的replace、replaceEach、replaceEachRepeatedly、replaceFirst方法详解

2018-02-06 19:33 489 查看

1.replace方法替换字符串中的某一部分字符串

/**
*
* 方法描述 替换指定字符串方法
*
* @param text--字符串
* @param searchString--被替换的字符串
* @param replacement--替换字符串
* @param max 替换字符串searchString的最大个数
* @param ignoreCase 是否忽略大小写
* @return
*
* @author yaomy
* @date 2018年2月6日 下午5:50:28
*/
private static String replace(String text, String searchString, String replacement, int max, boolean ignoreCase)
{
if ((isEmpty(text)) || (isEmpty(searchString)) || (replacement == null) || (max == 0)) {
return text;
}
String searchText = text;
//如果忽略大小写text、searchString都转换为小写
if (ignoreCase) {
searchText = text.toLowerCase();
searchString = searchString.toLowerCase();
}
int start = 0;
//searchString 的起始索引
int end = searchText.indexOf(searchString, start);
if (end == -1) {
return text;
}
//搜索字符串的长度
int replLength = searchString.length();
//搜索字符串和替换字符串的差值,如果小于0,字符串增量就为0
int increase = replacement.length() - replLength;
increase = increase < 0 ? 0 : increase;
increase *= (max > 64 ? 64 : max < 0 ? 16 : max);
StringBuilder buf = new StringBuilder(text.length() + increase);
while (end != -1) {
buf.append(text, start, end).append(replacement);
start = end + replLength;
max--; if (max == 0) {
break;
}
end = searchText.indexOf(searchString, start);
}
buf.append(text, start, text.length());
return buf.toString();
}


2.replace方法替换字符串中指定的字符并且指定替换的个数,区分大小写

public static String replace(String text, String searchString, String replacement, int max)
{
return replace(text, searchString, replacement, max, false);
}


3.replace方法替换字符串中指定的字符串,区分大小写,替换所有匹配到的字符串

public static String replace(String text, String searchString, String replacement)
{
return replace(text, searchString, replacement, -1);
}


4.replace方法替换掉所有正则表达式匹配的字符串

public static String replaceAll(String text, String regex, String replacement)
{
if ((text == null) || (regex == null) || (replacement == null)) {
return text;
}
return text.replaceAll(regex, replacement);
}


5.replaceEach和replaceEachRepeatedly使用数组批量指定要替换的字符串

//暂时未发现两个方法有什么异同
StringUtils.replaceEach(searchText, new String[] {"ni","ma","yao"}, new String[] {"NI","Ma","LL"})
StringUtils.replaceEachRepeatedly(searchText, new String[] {"ni","ma","yao"}, new String[] {"NI","Ma","LL"})


6.replaceFirst方法替换掉第一个匹配正则表达式的子字符串

public static String replaceFirst(String text, String regex, String replacement)
{
if ((text == null) || (regex == null) || (replacement == null)) {
return text;
}
return text.replaceFirst(regex, replacement);
}


7.replaceOnce方法替换掉字符串的子字符串中匹配到的字符串一次

public static String replaceOnce(String text, String searchString, String replacement)
{
return replace(text, searchString, replacement, 1);
}


8.replacePattern方法替换掉与正则表达式匹配的子字符串

public static String replacePattern(String source, String regex, String replacement)
{
if ((source == null) || (regex == null) || (replacement == null)) {
return source;
}
return Pattern.compile(regex, 32).matcher(source).replaceAll(replacement);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐