关于StringUtils的使用(BeanUtils.copyProperties()拷贝相同的bean属性)

1.导入依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.5</version>
</dependency>

2.java类上的导包名称

3.调用方法:

4.postman传参:

Spring工具类StringUtils常用方法
org.springframework.util.StringUtils(spring自带的包)

StringUtils常用方法    描述
booleanisEmpty(Object str)    判断字符串是否为空,如果为nul`或者`""则返回true,否则返回false
booleanhasLength(CharSequence str)    判断字符串是否有长度,字符串不等于null且长度大于0,则为true,否则返回false
booleanhasText(CharSequence str)    如果字符序列中有一个不是空白"",返回true,否则返回false(空格字符串也是)
booleancontainsWhitespace(CharSequence str)    判断字符序列是否包含空白,有一个字符是空白,返回true,否则返回false
String trimWhitespace(String str)    用于去除字符串前后的空白
String trimAllWhitespace(String str)    去除字符串所有空白
String trimLeadingWhitespace(String str)    去除字符串左边的空白字符
String trimTrailingWhitespace(String str)    去除字符串右边边的空白字符
trimLeadingCharacter(String str, char leadingCharacter)    删除字符串左边为leadingCharacter的字符
String trimTrailingCharacter(String str, char trailingCharacter)    删除字符串右边为trailingCharacter的字符
startsWithIgnoreCase(String str, String prefix)    忽略大小写,然后判断字符串是否已prefix前缀开始
booleanendsWithIgnoreCase(String str, String suffix)    忽略大小写,判断源字符串是否以suffix结尾
String getFilename(String path)    获取文件名
String getFilenameExtension(String path)    获取文件扩展名
String stripFilenameExtension(String path)    去除文件扩展名
String replace(String inString, String oldPattern, String newPattern)    替换字符串,参1:字符串,参2:需要替换的字符串,参3:用来替换的字符串
String delete(String inString, String pattern)    从给定的字符串中删除所有匹配的字符
String deleteAny(String inString, String charsToDelete)    删除所有指定字符
————————————————
版权声明:本文为CSDN博主「strive_day」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40542534/article/details/110962293

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以将循环中的复杂逻辑提取出来,提高代码的可读性和复用性。例如,定义一个方法来根据用户id或者组织代码获取对应的列权限列表,代码如下: ``` private List<SchemaColumnRule> getColumnRuleList(List<SchemaColumnRuleEo> schemaColumnRuleList, String userId, String orgCode) { List<SchemaColumnRule> userColumnRuleList = new ArrayList<>(); for (SchemaColumnRuleEo schemaRowRuleEo : schemaColumnRuleList) { if(StringUtils.isNotEmpty(schemaRowRuleEo.getColumnPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getColumnPermissionUserList(), String.class); if(userlist.contains(userId)){ SchemaColumnRule schemaColumnRule = SchemaColumnRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaColumnRule); userColumnRuleList.add(schemaColumnRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getColumnPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getColumnPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ SchemaColumnRule schemaColumnRule = SchemaColumnRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaColumnRule); userColumnRuleList.add(schemaColumnRule); } } } return userColumnRuleList; } ``` 然后在主函数中调用该方法: ``` DataPermissionSchema build = DataPermissionSchema.builder().build(); build.setSchemaRowRuleList(userRowRuleList); List<SchemaColumnRule> userColumnRuleList = getColumnRuleList(schemaColumnRuleList, userId, orgCode); build.setSchemaColumnRuleList(userColumnRuleList); ``` 这样代码更加简洁易懂,也方便后续重复使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值