增加字符串倒序重复字符约束条件

This commit is contained in:
wanggeng 2022-02-12 16:53:15 +08:00
parent ff5c06ec45
commit eee23bdd1e

View File

@ -398,4 +398,26 @@ public class WStringUtil {
return str.substring(0, unRepeatCharIndex + 1);
}
/**
* 倒叙截取重复字段
*
* @param str
* @param repeatChar
* @param minRepeatCount
* @return
*/
public static String cutContinuityRepeatCharDesc(String str, char repeatChar, int minRepeatCount) {
int repeatCount = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) != repeatChar) {
break;
}
repeatCount++;
}
if (repeatCount < minRepeatCount) {
return str;
}
return cutContinuityRepeatCharDesc(str, repeatChar);
}
}