mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
d6fe591abd
commit
50fe0f4fce
@ -206,19 +206,37 @@ public class ReUtil {
|
|||||||
* @return 匹配后得到的字符串数组,按照分组顺序依次列出,未匹配到返回空列表,任何一个参数为null返回null
|
* @return 匹配后得到的字符串数组,按照分组顺序依次列出,未匹配到返回空列表,任何一个参数为null返回null
|
||||||
* @since 4.0.13
|
* @since 4.0.13
|
||||||
*/
|
*/
|
||||||
public static List<String> getAllGroups(final Pattern pattern, final CharSequence content, final boolean withGroup0) {
|
public static List<String> getAllGroups(Pattern pattern, CharSequence content, boolean withGroup0) {
|
||||||
|
return getAllGroups(pattern, content, withGroup0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得匹配的字符串匹配到的所有分组
|
||||||
|
*
|
||||||
|
* @param pattern 编译后的正则模式
|
||||||
|
* @param content 被匹配的内容
|
||||||
|
* @param withGroup0 是否包括分组0,此分组表示全匹配的信息
|
||||||
|
* @param findAll 是否查找所有匹配到的内容,{@code false}表示只读取第一个匹配到的内容
|
||||||
|
* @return 匹配后得到的字符串数组,按照分组顺序依次列出,未匹配到返回空列表,任何一个参数为null返回null
|
||||||
|
* @since 4.0.13
|
||||||
|
*/
|
||||||
|
public static List<String> getAllGroups(Pattern pattern, CharSequence content, boolean withGroup0, boolean findAll) {
|
||||||
if (null == content || null == pattern) {
|
if (null == content || null == pattern) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<String> result = new ArrayList<>();
|
ArrayList<String> result = new ArrayList<>();
|
||||||
final Matcher matcher = pattern.matcher(content);
|
final Matcher matcher = pattern.matcher(content);
|
||||||
if (matcher.find()) {
|
while (matcher.find()) {
|
||||||
final int startGroup = withGroup0 ? 0 : 1;
|
final int startGroup = withGroup0 ? 0 : 1;
|
||||||
final int groupCount = matcher.groupCount();
|
final int groupCount = matcher.groupCount();
|
||||||
for (int i = startGroup; i <= groupCount; i++) {
|
for (int i = startGroup; i <= groupCount; i++) {
|
||||||
result.add(matcher.group(i));
|
result.add(matcher.group(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(false == findAll){
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -174,11 +174,24 @@ public class PageUtil {
|
|||||||
* @param pageSize 每页数
|
* @param pageSize 每页数
|
||||||
* @return 总页数
|
* @return 总页数
|
||||||
*/
|
*/
|
||||||
public static int totalPage(final int totalCount, final int pageSize) {
|
public static int totalPage(int totalCount, int pageSize) {
|
||||||
|
return totalPage((long) totalCount, pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据总数计算总页数
|
||||||
|
*
|
||||||
|
* @param totalCount 总数
|
||||||
|
* @param pageSize 每页数
|
||||||
|
* @return 总页数
|
||||||
|
* @since 5.8.5
|
||||||
|
*/
|
||||||
|
public static int totalPage(long totalCount, int pageSize) {
|
||||||
if (pageSize == 0) {
|
if (pageSize == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
|
return Math.toIntExact(totalCount % pageSize == 0
|
||||||
|
? (totalCount / pageSize) : (totalCount / pageSize + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,7 +111,7 @@ public class ReUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void replaceAllTest2() {
|
public void replaceAllTest2() {
|
||||||
//此处把1234替换为 ->1234<-
|
//此处把1234替换为 ->1234<-
|
||||||
final String replaceAll = ReUtil.replaceAll(this.content, "(\\d+)", parameters -> "->" + parameters.group(1) + "<-");
|
final String replaceAll = ReUtil.replaceAll(content, "(\\d+)", parameters -> "->" + parameters.group(1) + "<-");
|
||||||
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
|
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user